简体   繁体   English

jscript不能正常工作

[英]jscript just not working

After many days of searching and testing I cannot solve this mystery of why a simple mobile redirect is not redirecting. 经过数天的搜索和测试,我无法解决这个简单的移动重定向无法重定向的谜团。 I have tested several variations from across the web with no avail. 我已经测试了网络上的多种变体,但无济于事。 If anyone has a moment to look at what I have, please do. 如果有人有时间看看我有什么,请做。 Thanks in advance. 提前致谢。

My goal: Redirect site to mobile site upon detection of user agent. 我的目标:在检测到用户代理后将站点重定向到移动站点。

My code is below. 我的代码如下。 NOTE: if I change 注意:如果我改变

if(isMobile.any()) {

to

if(isMobile) {

the desktop browser will redirect the site. 桌面浏览器将重定向站点。

<!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>

The code seemed to work for me. 该代码似乎对我有用。

I created this fiddle: http://jsfiddle.net/dkemvuyu/1/ 我创建了这个小提琴: http : //jsfiddle.net/dkemvuyu/1/

But just replaced the 'if' with: 但是只需将“ if”替换为:

if(isMobile.any()) {
   alert('Device is a mobile');
} else{
   alert('Device is NOT a mobile');
}

and then navigated to it with my iphone and the alert came up as expected (telling me I was using a mobile device). 然后使用我的iphone导航到它,并按预期出现警报(告诉我我正在使用移动设备)。

All I can assume is that the mobiletest.me website does not send the useragent header as part of the request that corresponds with the device to the website (ps I also tested navigating to the fiddle via the mobiletest.me website and it told me I was not a mobile device which confirms what I thought). 我可以假设的是,mobiletest.me网站没有将与该设备相对应的请求的一部分发送到该网站的useragent标头(我还测试了通过mobiletest.me网站导航到小提琴的提示,并告诉我不是确认我的想法的移动设备)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM