重定向旧浏览器Safari用户

Redirect Old Browers Safari Users

本文关键字:用户 Safari 浏览器 重定向      更新时间:2023-09-26

我正在使用此代码重定向safari用户。。。

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

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

<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)"],因此safari[1]将以字符串形式提供版本号。然后,您可以尝试将该值解析为float(或int)以获取版本号,然后根据结果进行重定向。

我已经设法创建了一个解决方案来解决@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>