简体   繁体   中英

javascript user agent redirect by browser version number

We have a chat program that works with only a couple of browsers right now. So, I'm inserting a user agent redirect to manage the messaging to inform the user why they can't chat with their unsupported browser.

The issue I'm having is only Firefox 3.1 and under, for example, is supported for FireFox., but my custom script below is enabling all Firefox versions compatible. What's the solution to have only Firefox 3.1 be compatible?

Note: I don't plan to send them to the actual browser websites as seen in my example. I just put those URLs in for example purposes only. I plan to have custom redirect pages with friendly messaging on them...

Demo of existing code: http://jsfiddle.net/evanmoore/4xr77/

Code is below:

<script type="text/javascript">
    if ((navigator.userAgent.indexOf('Firefox') != -1) || (navigator.userAgent.indexOf('MSIE') != -1)) 
    {
        // Your browser is supported for live chat
        document.location = "http://www.livechatinc.com/";
    }
    else if(navigator.userAgent.indexOf("Safari") != -1)
    {
        // Your Safari browser is not supported for live chat
        window.location = "http://www.apple.com";
    }
    else if(navigator.userAgent.indexOf("Chrome") != -1)
    {
        // Your Chrome browser is not supported for live chat
        window.location = "http://www.google.com/chrome";
    }
    else 
    {   // All others... Your browser is not supported for live chat
        window.location = "http://www.gofetch.com";
    }
</script>

Based on Asad's comment, I found the different browser strings here which gave me the ability to control the version number like so... I think this should do the trick!

if ((navigator.userAgent.indexOf('Firefox/3.1') != -1) 

Try checking if the functionality exists, not the version of the browser.
eg if (typeof foo != 'undefined') will check if foo exists
You can find more info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM