javascript user agent redirect by browser version number

半城伤御伤魂 提交于 2019-12-08 08:39:44

问题


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>

回答1:


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) 



回答2:


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



来源:https://stackoverflow.com/questions/6719118/javascript-user-agent-redirect-by-browser-version-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!