JavaScript to judge IE browser 6/7 version load different style sheet implementation code _javascript tips

Source: Internet
Author: User
Key points: 1, the browser version of the judgment; 2, modify style sheet path
The 2nd is also commonly used in real-time modify the page template, forum style occasions, is actually modifying the style sheet path to load different style sheets.
Code:
Copy Code code as follows:

<script type= "Text/javascript" >
var browser=navigator.appname
var b_version=navigator.appversion
var version=b_version.split (";");
try{//code is only valid for IE browser, in order to avoid the error in other browsers, you can use the try{code body}catch (ERR) {code Body} to eliminate the error
var trim_version=version[1].replace (/[]/g, "");
if (browser== "Microsoft Internet Explorer" && trim_version== "MSIE7.0")
{
Replace "Your-stylesheet-url" with the path of the style sheet Chengxiang loaded in IE7
Document.stylesheets[0].href= "Your-stylesheet-url"; Key points for modifying style sheet paths: document.stylesheets[0].href
}
else if (browser== "Microsoft Internet Explorer" && trim_version== "MSIE6.0")
{
Replace "Your-another-stylesheet-url" with the path of the style sheet Chengxiang loaded in IE6
Document.stylesheets[0].href= "Your-another-stylesheet-url";
}
}
catch (Err) {//Catch error without any processing can also
}
</script>

Practical JS to determine browser type and version
Code:
Copy Code code as follows:

<title> Practical JS judgment Browser type and version </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<script language= "JavaScript" >
var imyeah={};
var ua=navigator.useragent.tolowercase ();
var s;
(S=ua.match (/msie) ([\d.] +)/)) ? IMYEAH.IE=S[1]:
(S=ua.match (/firefox\/) ([\d.] +)/)) ? IMYEAH.FIREFOX=S[1]:
(S=ua.match (/chrome\/) ([\d.] +)/)) ? IMYEAH.CHROME=S[1]:
(S=ua.match (/opera.) ( [\d.] +)/)) ? IMYEAH.OPERA=S[1]:
(S=ua.match (/version\/) ([\d.] +). *safari/)? IMYEAH.SAFARI=S[1]: 0;

The following tests
if (imyeah.ie) document.write (' ie: ' +imyeah.ie);
if (Imyeah.firefox) document.write (' Firefox: ' +imyeah.firefox);
if (imyeah.chrome) document.write (' Chrome: ' +imyeah.chrome);
if (Imyeah.opera) document.write (' Opera: ' +imyeah.opera);
if (Imyeah.safari) document.write (' Safari: ' +imyeah.safari);
</script>
<body>
</body>

This code is very short, but it's very useful to be able to accurately determine ie, FF, Chrome, Opera, Safari browsers, and their versions.
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.