Detect whether browser is IE 7 or lower?

后端 未结 6 2028
萌比男神i
萌比男神i 2020-12-15 07:46

So i am going to add a redirect to my site to toss every one that is using ie 7 or lower off to a different page and came up with this JavaScript, but it seems to have stopp

相关标签:
6条回答
  • 2020-12-15 08:20

    Conditional comments (as suggested by @Kon) are the way to go. Here's a working implementation:

    <script type="text/javascript">
        var ie7OrLower = false;
    </script>
    
    <!--[if lte IE 7]><script type="text/javascript">
       ie7OrLower = true;
    </script><![endif]-->
    
    <script type="text/javascript">
        window.location = ie7OrLower ? "ie.html" : "main.html";
    </script>
    
    0 讨论(0)
  • 2020-12-15 08:25

    Check out conditional comments.

    So you can do something like:

    <script type="text/javascript">
        <!--[if (!IE)|(gt IE 7)]>
          window.location = "ie.html" 
        <![endif]-->
    
        <!--[if lt IE 8]>
          window.location = "main.html"
        <![endif]-->
    </script>
    
    0 讨论(0)
  • 2020-12-15 08:28

    I've always used Quirks Mode's BrowserDetect.js for my browser detection needs. Check it out - http://www.quirksmode.org/js/detect.html

    Once you've referenced the .js file, you can access lots of information:

    //Browser Name
    BrowserDetect.browser
    //Browser Version
    BrowserDetect.version
    //Operating system
    BrowserDetect.OS
    
    0 讨论(0)
  • 2020-12-15 08:29

    Your code is always resulting to having gone to main.html. Even when the code falls into <8, you'll fall out of the if into setting to main.

    Consider refactoring by either:

    • setting a return after setting to ie.

    or

    var redir="main.html";
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
    { 
       var ieversion=new Number(RegExp.$1);
       if (ieversion<=8)
       {
          redir = "ie.html";
       }
    }
    window.location = redir;
    
    0 讨论(0)
  • 2020-12-15 08:39

    I'd just use the examples at http://www.ie6nomore.com/

    0 讨论(0)
  • 2020-12-15 08:47

    You can test it with this regular expression: (MSIE\ [0-7]\.\d+)

    Here is a JavaScript example on how to use it:

    if (/(MSIE\ [0-7]\.\d+)/.test(navigator.userAgent)) {
        // do something
    }
    
    0 讨论(0)
提交回复
热议问题