重定向旧浏览器Safari用户

时间:2015-11-05 18:12:20

标签: javascript html redirect browser safari

我使用此代码重定向safari用户......

<script>
    var uagent = navigator.userAgent.toLowerCase();
    if(/safari/.test(uagent) && !/chrome/.test(uagent))
    {
        window.location.href = "http://www.google.com"
    }
</script>

但我需要一个脚本来重定向旧的Safari浏览器,有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

'tag' => 'javascript',

<script type="text/javascript"> window.onload = function () { var uagent = navigator.userAgent.toLowerCase(); var safari = uagent.match(/safari\/(\S+)/); var chrome = uagent.match(/chrome\/(\S+)/); var MIN_SAFARI_VERSION = 900; if(safari && !chrome) { if (parseFloat(safari[1]) < MIN_SAFARI_VERSION) { window.location.href = "http://www.google.com" } } } </script> 的值为safari,因此["safari/(version number)", "(version number)"]会将版本号作为字符串提供给您。然后,您可以尝试将该值解析为float(或int)以获取版本号,然后根据结果重定向。

答案 1 :(得分:0)

我设法创建了解决@evan_schmevan答案问题的解决方案,因为它对我不起作用。

我的解决方案在uagent上以不同的格式运行。

希望它有所帮助。试试片段

window.onload = function () {
        var uagent = navigator.userAgent.toLowerCase();
        var MIN_SAFARI_VERSION = 11;
        console.log ("Your uagent: ||"+ uagent+"||");

        //First, check if safari appears on the user agent
        var safari = uagent.match(/safari\/(\S+)/);
        
        //Chrome shows SAFARI on its user agent, when used from              //mac, then whe check this to ensure is not chrome
        var chrome = uagent.match(/chrome\/(\S+)/);

        //If safari is detected on uagent
        if(safari.length > 0 && !chrome  ) {
            console.log("Safari detected");
            var version = uagent.match(/version\/([0-9]+)/);
            console.log("Version detected: "+version[1]);
            if(version[1] < MIN_SAFARI_VERSION) {
            console.log ("OOPS!!! Version is lower than min required");
            //window.location.href = "http://www.google.com"
            }
         }
            else if (chrome.length>0)
            {
              console.log("Chrome detected");
            }
        
    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html>
<body>
<h1> Detect your browser version</h1>
</body>
</html>