Javascript judges the implementation code of loading different style sheets in IE browser 6/7

Source: Internet
Author: User

Key points: 1. Determine the browser version; 2. Modify the style sheet path
The second point is often used when you modify webpage templates and Forum styles in real time. In fact, you can modify the style sheet path to load different style sheets.
Code: Copy codeThe Code is as follows: <script type = "text/javascript">
Var browser = navigator. appName
Var B _version = navigator. appVersion
Var version = B _version.split (";");
Try {// The code is only valid for IE browsers. To avoid errors reported in other browsers, try {code body} catch (err) {code body} can be used to eliminate errors.
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 table to be loaded in ie7
Document. styleSheets [0]. href = "your-stylesheet-url"; // you can modify the path of the style sheet at the key points: 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 to be loaded in ie6
Document. styleSheets [0]. href = "your-another-stylesheet-url ";
}
}
Catch (err) {// catch errors without any processing
}
</Script>

Practical js judgment of browser types and versions
Code:Copy codeThe Code is as follows: <Head>
<Title> practical js judgment of browser types and versions </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;

// Perform 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>
</Head>
<Body>
</Body>
</Html>

This code is very short, but it is very practical to accurately judge ie, FF, Chrome, Opera, Safari browser and its version.

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.