Javascript移动重定向问题
大家好,这已经让我头疼了好几天了,我想重定向到移动网站,让那些从屏幕宽度低于699的设备上查看网站的人可以使用。我正在使用此脚本进行重定向:Javascript移动重定向问题,javascript,redirect,mobile,Javascript,Redirect,Mobile,大家好,这已经让我头疼了好几天了,我想重定向到移动网站,让那些从屏幕宽度低于699的设备上查看网站的人可以使用。我正在使用此脚本进行重定向: <script type="text/javascript"> <!-- if (screen.width <= 699) { window.location = "http://www.mywebsite.com.au/mobile/"; } //--> </script> 当我通过firefox检查站点时
<script type="text/javascript">
<!--
if (screen.width <= 699) {
window.location = "http://www.mywebsite.com.au/mobile/";
}
//-->
</script>
当我通过firefox检查站点时,脚本工作正常,但在Dolphin浏览器中不工作
可能只有我的手机我有Galaxy S2
提前感谢任何能帮助我的人,我真的很感激
新的更新:--好的,这变得非常有趣。当我将屏幕宽度减小到599时,脚本在Dolpin浏览器中工作。(screen.widthwindow.location是一个对象,因此您可以尝试以下操作:
window.location.replace("http://www.mywebsite.com.au/mobile/");
请参见我建议使用用户代理检测进行类似的操作
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/";
}
我的手机(魅族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>
var userAgent=navigator.userAgent.toLowerCase();
checkOS=函数(r){
返回r.test(userAgent);
};
变量平台={
iWindows:checkOS(/windows nt | win32/),
isMac:checkOS(/macintosh | MacOSX/),
isAndroidPad:checkOS(/nexus 7 | xoom/),
isAndroid:checkOS(/android/),
isIphone:checkOS(/iphone/),
isIpad:checkOS(/ipad/),
iWindowsPhone:checkOS(/windows phone/),
操作系统:“”,
}
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=函数(){
var currWidth=document.body.clientWidth;
如果(电流宽度>=699)
location.href=”http://www.mywebsite.com.au/pc/";
其他的
location.href=”http://www.mywebsite.com.au/mobile/";
}
由于我对手机的了解不够,我建议在服务器端通过勾选“客户端”来实现这一点操作系统或用户代理,而不是在javascript中执行此操作。Kiran,这是否也会将ipad用户重定向到移动站点?我希望ipad用户转到完整站点。在这种情况下,从列表中删除ipad。kiranvj向您展示了如何使用简单的正则表达式选择哪些类型的设备将重定向到移动站点。通过对不同的useragent头不同的设备和浏览器发送http请求,您可以完全控制将哪些浏览器和设备重定向到您的移动站点。这里有一个详尽的列表:我个人使用此字符串检测移动设备:var mobile=(/iphone | ipad | ipod | android | blackberry | mini | windows\sce | palm/i.test(navigator.userAgent.toLowerCase());
也许我应该像kiranvj那样添加symbian。