1

我正在尝试让我的在线帮助重定向到手机版本。但是,我是从单一来源生成手机和桌面版本的,所以我想要一段代码来完成这一切(手机 <-769-> 桌面)。由于大小发生使用,我得到了重定向:

if (screen.width <= 769 || windowWidth <= 769) {
window.location = "../phone/Selecting_a_School_Register.htm";

但是当我在电话页面上时,它一直在尝试加载页面(我明白为什么)。我自己尝试过,但我对此并不陌生,这是我的(失败的)尝试:

windowWidth = window.innerWidth;
<!--
if (location.pathname = "/desktop/Selecting_a_School_Register.htm";) 
{
} else if (screen.width <= 769 || windowWidth <= 769) {
window.location = "../phone/Selecting_a_School_Register.htm";

}
</script><script>
if (location.pathname = "/phone/Selecting_a_School_Register.htm";) 
{
} else if (screen.width >= 769 || windowWidth >= 769) {
window.location = "../desktop/Selecting_a_School_Register.htm";

}
4

2 回答 2

1

任何类型的条件语句都需要一个双 '==' 而不是像您的代码示例所暗示的单个 '='。此外,您的 if 语句中的字符串后不需要分号。

比双等号更好的是三等号,它是一个严格的比较运算符

尝试这个:

windowWidth = window.innerWidth;

if (location.pathname === "/desktop/Selecting_a_School_Register.htm") {
} 
else if (screen.width <= 769 || windowWidth <= 769) {
    window.location = "../phone/Selecting_a_School_Register.htm";
}

if (location.pathname === "/phone/Selecting_a_School_Register.htm") {
} 
else if (screen.width >= 769 || windowWidth >= 769) {
    window.location = "../desktop/Selecting_a_School_Register.htm";
}

编辑:

此代码完全符合您的需要:

var pathname = window.location.pathname
  , width = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;

if (width <= 769 && pathname !== '/mobile.html') {
    window.location = '/mobile.html';
}

if (width > 769 && pathname !== '/desktop.html') {
    window.location = '/desktop.html';
}
于 2013-10-17T11:17:58.577 回答
0

你好,

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());
    }
};

Example :

To check to see if the user is on any of the supported mobile devices:

if( isMobile.any() ) alert('Mobile');

To check to see if the user is on a specific mobile device:

if( isMobile.iOS() ) alert('iOS');

谢谢你,维沙尔·帕特尔

于 2013-10-17T14:20:17.203 回答