Javascript移动重定向问题

Javascript mobile redirect issue

本文关键字:问题 重定向 移动 Javascript      更新时间:2023-09-26

嗨,这已经做了几天我的头现在,我想重定向到移动网站,从他们的屏幕宽度低于699的设备检查网站。我使用这个脚本进行重定向:

<script type="text/javascript">
<!--
if (screen.width <= 699) {
window.location = "http://www.mywebsite.com.au/mobile/";
}
//-->
</script>

脚本工作正常,当我通过firefox检查网站,但不工作在海豚浏览器..

可能只是我的手机,我有Galaxy S2。

提前感谢任何人谁可以帮助我这个,我真的很感激!

新更新:-----好吧,这变得非常有趣。当我将屏幕宽度减小到599时,脚本可以在Dolpin浏览器中工作。

我建议使用用户代理检测

var isMobile = function() {
   //console.log("Navigator: " + navigator.userAgent);
   return /(iphone|ipod|ipad|android|blackberry|windows ce|palm|symbian)/i.test(navigator.userAgent);
 };

像这样重定向

if(isMobile()) {
       window.location.href = "http://www.mywebsite.com.au/mobile/";
}

窗口。Location是一个对象,所以你可以试试下面的命令:

window.location.replace("http://www.mywebsite.com.au/mobile/");

看到window.location.replace

我的手机(魅族MX2)运行良好。我不知道海豚浏览器的版本。你可以测试'screen.width'和'document.body. clientwidth '

我建议你这样写:

<script>     
    var userAgent = navigator.userAgent.toLowerCase();
    checkOS = function (r) {
        return r.test(userAgent);
    };
    var PlatformOS = {
        isWindows: checkOS(/windows nt|win32/),
        isMac: checkOS(/macintosh|mac os x/),
        isAndroidPad: checkOS(/nexus 7|xoom /),
        isAndroid: checkOS(/android/),
        isIphone: checkOS(/iphone/),
        isIpad: checkOS(/ipad/),
        isWindowsPhone: checkOS(/windows phone/),
        OS: "",
    }
    if (PlatformOS.isIpad || PlatformOS.isWindows || PlatformOS.isAndroidPad) {
        location.href = "http://www.mywebsite.com.au/pc/";
    }
    else if (PlatformOS.isIphone||PlatformOS.isWindowsPhone) {
        location.href = "http://www.mywebsite.com.au/mobile/";
    }
    window.onload = function () {            
        var currWidth = document.body.clientWidth;
        if (currWidth >= 699)
            location.href = "http://www.mywebsite.com.au/pc/";
        else
            location.href = "http://www.mywebsite.com.au/mobile/";
    }
</script>