Computer Browser and Mobile browser

Source: Internet
Author: User

<script type= "Text/javascript" >

<!--//platform, device and operating system VAR system ={win:false,mac:false,xll:false};//detection Platform var p = Navigator.platform;//alert (p); SYSTEM.W in = P.indexof ("Win") = = 0;system.mac = P.indexof ("mac") = = = 0;system.x11 = (p = = "X11") | | (P.indexof ("Linux") = = 0);//Jump statement if (system.win| | system.mac| | SYSTEM.XLL) {//Turn to Background landing page document.write (');} Else{document.write (');} --></script>

<!--
if (navigator.userAgent.indexOf (' Ucbrowser ') >-1) {
alert (123);

}-->//-------

Many browsers use unified functionality, and the way they are implemented differs depending on the core of the browser. So sometimes we need to detect the browser.

A The problem lies

In the basic classroom, we adopt two kinds of schemes, one is direct supply, one is analysis. Both of these schemes are cumbersome, but relatively delicate, and in the actual use, it is not necessary.

Two Setup Code

Browser Detection

(function () {

Window.sys = {};

var ua = Navigator.userAgent.toLowerCase ();

var s;

if (/msie ([\d.] +)/). Test (UA)) {//To determine IE Browser

s = Ua.match (/msie ([\d.] +)/);

sys.ie = s[1];

}

if (/firefox\/([\d.] +)/). Test (UA)) {//to determine Firefox browser

s = Ua.match (/firefox\/([\d.] +)/);

Sys.firefox = s[1];

}

if (/chrome\/([\d.] +)/). Test (UA)) {// judge Google Chrome

s = Ua.match (/chrome\/([\d.] +)/);

Sys.chrome = s[1];

}

if (/opera.*version\/([\d.] +)/). Test (UA)) {// judge opera browser

s = Ua.match (/opera.*version\/([\d.] +)/);

Sys.opera = s[1];

}

if (/version\/([\d.] +). *safari/). Test (UA) {// judge Safari Browser

s = Ua.match (/version\/([\d.] +). *safari/);

Sys.safari = s[1];

}

alert (sys.ie);

})();

PS: The above notation contains a lot of duplicate code, we do a bit of compression.

Browser Detection

(function () {

Window.sys = {};

var ua = Navigator.userAgent.toLowerCase ();

var s;

(s = Ua.match (/msie ([\d.] +)/)) ? sys.ie = S[1]:

(s = Ua.match (/firefox\/([\d.] +)/)) ? Sys.firefox = S[1]:

(s = Ua.match (/chrome\/([\d.] +)/)) ? Sys.chrome = S[1]:

(s = Ua.match (/opera.*version\/([\d.] +)/)) ? Sys.opera = S[1]:

(s = Ua.match (/version\/([\d.] +). *safari/))? Sys.safari = S[1]: 0;

})();

Computer Browser and Mobile browser

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.