Javascript jscript只是不起作用

Javascript jscript只是不起作用,javascript,redirect,Javascript,Redirect,经过许多天的搜索和测试,我无法解开为什么简单的移动重定向不是重定向的谜团。我已经在网上测试了几种变体,但都没有用。如果有人有时间看我的东西,请看。提前谢谢 我的目标:在检测到用户代理后将站点重定向到移动站点 我的代码如下。注意:如果我改变 if(isMobile.any()) { 到 桌面浏览器将重定向站点 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <scr

经过许多天的搜索和测试,我无法解开为什么简单的移动重定向不是重定向的谜团。我已经在网上测试了几种变体,但都没有用。如果有人有时间看我的东西,请看。提前谢谢

我的目标:在检测到用户代理后将站点重定向到移动站点

我的代码如下。注意:如果我改变

if(isMobile.any()) {

桌面浏览器将重定向站点

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
    var isMobile = {
Android: function() {
    return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
    return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
    return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
    return navigator.userAgent.match(/IEMobile/i);
    },
any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

if(isMobile.any()) {
   { window.location = 'http://www.briggsmechanical.com/mobile/index.html';
}
}
    </script>

var isMobile={
Android:function(){
返回navigator.userAgent.match(/Android/i);
},
黑莓:函数(){
返回navigator.userAgent.match(/BlackBerry/i);
},
iOS:function(){
返回navigator.userAgent.match(/iPhone | iPad | iPod/i);
},
Opera:函数(){
返回navigator.userAgent.match(/Opera-Mini/i);
},
Windows:function(){
返回navigator.userAgent.match(/IEMobile/i);
},
any:function(){
返回(isMobile.Android()| | isMobile.BlackBerry()| | isMobile.iOS()| | isMobile.Opera()| | isMobile.Windows());
}
};
if(isMobile.any()){
{window.location=http://www.briggsmechanical.com/mobile/index.html';
}
}

代码似乎对我有用

我创作了这把小提琴:

但只是将“如果”替换为:

if(isMobile.any()) {
   alert('Device is a mobile');
} else{
   alert('Device is NOT a mobile');
}
然后用我的iphone导航到它,警报按预期出现(告诉我我正在使用移动设备)


我只能假设mobiletest.me网站没有将useragent头作为与设备对应的请求的一部分发送到网站(注意,我还测试了通过mobiletest.me网站导航到小提琴,它告诉我我不是一个移动设备,这证实了我的想法).

移动设备是否有任何错误消息不起作用?如果(isMobile)出现,则它之所以“起作用”,是因为isMobile是一个对象,因此不为false,因此表达式的计算结果为true。p.s。如今,移动特定网站已经不是必要的了,有了可用的东西,你可以编写一个适合设备宽度的网站。我一直在使用“mobiletest.me”检查移动设备,所以我没有收到任何错误消息。Magrangs-是的,但我希望更具体的(isMobile.any)会起作用。是的,bootstrap是一种方式,但这个网站是旧的html,这就是为什么我必须使用jscript并创建一个单独的移动网站。谢谢Magrangs。您的假设是正确的,因为移动测试站点不读取useragent。这完全是我的失误,因为我刚刚在一个真正的移动设备上打开了这个网站,一切都如期进行。(摇头)
if(isMobile.any()) {
   alert('Device is a mobile');
} else{
   alert('Device is NOT a mobile');
}