Js determines whether the current page is opened on a mobile device or on a PC. jspc

Source: Internet
Author: User
Tags set cookie

Js determines whether the current page is opened on a mobile device or on a PC. jspc

This article describes the code used by js to determine whether the current page is opened on a mobile device or on a PC. The details are as follows:

Var browser = {versions: function () {var u = navigator. userAgent, app = navigator. appVersion; return {// mobile terminal browser version information trident: u. indexOf ('think')>-1, // IE kernel presto: u. indexOf ('presto ')>-1, // opera kernel webKit: u. indexOf ('applewebkit ')>-1, // Apple, Google kernel gecko: u. indexOf ('gecko ')>-1 & u. indexOf ('khtml ') =-1, // Firefox kernel mobile :!! U. match (/AppleWebKit. * Mobile. */), // whether it is a Mobile terminal ios :!! U. match (/\ (I [^;] +; (U ;)? CPU. + Mac OS X/), // ios terminal android: u. indexOf ('android')>-1 | u. indexOf ('linux ')>-1, // android terminal or uc browser iPhone: u. indexOf ('iphone ')>-1, // whether the browser is iPhone or QQHD iPad: u. indexOf ('ipad ')>-1, // whether iPad webApp: u. indexOf ('safari ') =-1 // whether the web should program, no header and bottom};} (), language: (navigator. browserLanguage | navigator. language ). toLowerCase ()} if (browser. versions. mobile) {// determines whether a mobile device is enabled. The browser code is as follows: var ua = navigator. userAgent. toLowerCase (); // obtain the object if (ua. match (/MicroMessenger/I) = "micromessenger") {// open setInterval (WeixinJSBridge. call ('closewindow'), 2000);} if (ua. match (/WeiBo/I) = "weibo") {// open it on the Sina WeiBo client} if (ua. match (/QQ/I) = "qq") {// open in QQ space} if (browser. versions. ios) {// whether to open in IOS browser} if (browser. versions. android) {// whether to open the window in the android browser} else {// otherwise, the window is opened in the PC browser. close ();}

Code 2:Js determines whether a user's browsing device is a mobile device or a PC

In a recently created website page, different page styles need to be displayed based on the user's access device, mainly to determine whether the mobile device is accessed by a Computer Browser.

The js judgment and processing code is provided below for reference.

<Script type = "text/javascript"> function browserRedirect () {var sUserAgent = navigator. userAgent. toLowerCase (); var bIsIpad = sUserAgent. match (/ipad/I) = "ipad"; var bIsIphoneOs = sUserAgent. match (/iphone OS/I) = "iphone OS"; var bIsMidp = sUserAgent. match (/midp/I) = "midp"; var bIsUc7 = sUserAgent. match (/rv: 1.2.3.4/I) = "rv: 1.2.3.4"; var bIsUc = sUserAgent. match (/ucweb/I) = "ucweb"; var bIsAndroid = sUserAgent. match (/android/I) = "android"; var bIsCE = sUserAgent. match (/windows ce/I) = "windows ce"; var bIsWM = sUserAgent. match (/windows mobile/I) = "windows mobile"; document. writeln ("your browsing device is:"); if (bIsIpad | bIsIphoneOs | bIsMidp | bIsUc7 | bIsUc | bIsAndroid | bIsCE | bIsWM) {document. writeln ("phone");} else {document. writeln ("pc") ;}} browserRedirect (); </script>

I used browsers on my computer, android devices, iphone, and ipad to test the Code. This code is feasible and all devices are correctly judged.

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • How does Js determine whether the client is a PC or a handheld device for simple analysis?
  • Javascript allows video carousel on pc and mobile
  • Httpclient simulated login implementation (set cookie using js)
  • Javascript custom function to determine whether the Website access type is PC or mobile terminal
  • A Simple Method for js to determine whether a mobile device is logging on to a webpage
  • JavaScript code used to determine whether the client is a mobile phone or a PC
  • How does js determine whether users access data on PC or mobile?
  • Encapsulation instance of touch events in Web development of javascript mobile devices
  • Js Method for judging different execution events on mobile phones and PCs
  • JavaScript learning note detection client types are (engines, browsers, platforms, operating systems, mobile devices)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.