JavaScript Judge Mobile Browser instance code

Source: Internet
Author: User
Tags hash


Recently ready to do a mobile phone side of the site. So on the internet to find a few to determine whether it is a mobile phone browser JS script, posted up, share under:


1. How to determine whether to use the regular match for mobile terminals,

Matches whether navigator.useragent contains strings Applewebkit*****mobile

Android QQ Browser HD edition only AppleWebKit

2 Mobile language version of the judgment

Use Navigator.browserlanguage to get the Windows Phone language version,
Of course, hateful little phone. Language versions also have compatibility differences, compatible with Mozilla, and AppleWebKit kernel browsers to access their language versions, which list Navigator.language

First, Direct JS script:

The code is as follows Copy Code

try {
var urlhash = Window.location.hash;
if (!urlhash.match ("Fromapp")) {
if (Navigator.userAgent.match (/(iphone|ipod| Android|ios|ipad)/i)) {
window.location = "http://www.111cn.net (replace to own WAP website)";
}
}
The catch (err) {}

Second, reference (slightly longer):

  code is as follows copy code

function Uaredirect (murl) {
    try {
        if ( document.getElementById ("Bdmark")!= null) {
             return;
       }
        var urlhash = Window.location.hash;
        if (!urlhash.match ("Fromapp")) {
             if (Navigator.userAgent.match (/iphone|ipod| Android|ios|ipad)) {
                 Location.replace (Murl);
           }
       }
   } catch (Err) {}

The second way, page calls:


The above script is persisted to the Wap.js file, and then refers to the following 2 JS call files in the page that needs to be invoked.

The code is as follows Copy Code

<script src= "Wap.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >uaredirect ("http://www.111cn.net (replace to own WAP site)");</script>


Refer to the PHP code modified by the Thinkphp official web site:

The code is as follows Copy Code

<script type= "Text/javascript" >
var ua = navigator.useragent,
Mobileagents = ["240x320", "Acer", "Acoon", "acs-", "Abacho", "Ahong", "Airness", "Alcatel", "Amoi", "Android", " Anywhereyougo.com "," applewebkit/525 ",

"applewebkit/532", "Asus", "Audio", "Au-mic", "Avantogo", "Becker", "BenQ", "Bilbo", "Bird", "BlackBerry", "Blazer", " Bleu "," cdm-"," Compal "," Coolpad "," Danger "," Dbtel "," Dopod "," Elaine "," Eric "," Etouch "," Fly "," Fly_ "," fly-"," Go.web " , "Goodaccess", "Gradiente", "Grundig", "Haier", "Hedy", "Hitachi", "HTC", "Huawei", "Hutchison",

"Inno", "ipad", "iPAQ", "ipod", "Jbrowser", "KDDI", "kgt", "KWC", "Lenovo", "LG", "Lg2", "Lg3", "Lg4", "Lg5", "Lg7", "Lg8", " Lg9 "," lg-"," lge-"," Lge9 "," Longcos "," Maemo "," Mercator "," Meridian "," Micromax "," MIDP "," Mini "," Mitsu "," MMM "," MMP ", "Mobi", "mot-", "Moto", "nec-", "NetFront", "Newgen", "Nexian", "Nf-browser", "Nintendo", "Nitro", "Nokia", "Nook", " Novarra "," Obigo "," Palm "," Panasonic "," Pantech "," Philips "," Phone "," pg-"," PlayStation "," Pocket "," pt-"," qc-"," Qtek "," Rover "," Sagem "," sama "," Samu "," Sanyo "," Samsung "," sch-"," Scooter "," sec-"," Sendo "," sgh-"," Sharp "," Siemens "," Sie-"," SoftBank "," Sony "," Spice "," Sprint "," SPV "," Symbian "," tablet "," Talkabout "," tcl-"," Teleca "," Telit "," Tianyu " , "tim-", "Toshiba", "TSM", "Up.browser", "Utec", "Utstar", "Verykool", "Virgin", "vk-", "Voda", "Voxtel", "VX", "WAP", " Wellco "," Wig Browser "," Wii "," Windows CE "," wireless "," XDA "," Xde "," ZTE "];
for (var i = 0; i < mobileagents.length; i++) {
if (Ua.tolowercase (). IndexOf (Mobileagents[i]) >-1) {
window.location.href = ' http://www.111cn.net ';
Break
}
}
</script>

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.