将jref / jQuery切换到桌面到移动设备的href url

时间:2013-05-10 12:49:25

标签: javascript jquery mobile

大家好我无法找到我想要做的确切事情,并且作为一个javascript业余爱好者我似乎无法纠结它。 基本上... 我将此脚本附加到我页面上的各种元素

$(document).ready(function() {
$("h1, h2, h5, .class1, .class2 #image1").click(function () {
    window.open('https://www.linkdesktop.com');
});

});

我想做的是: 如果在移动设备上那么切换www.linkdesktop.com到www.linkmobile.com

这可能吗,我是根据屏幕尺寸还是使用某种移动侦测脚本来做的?

答案非常感谢,非常感谢。


好的,谢谢你的回答

所以也许是这样的事情?

    var userAgent = window.navigator.userAgent;

$(document).ready(function() {
if( (Android|webOS|iPhone|iPad|iPod|BlackBerry).test(navigator.userAgent) ) {

$("h1, h2, h5, .class1, .class2 #image1").click(function () {
    window.open('https://www.linkmobile.com');
});

}
else {

$("h1, h2, h5, .class1, .class2 #image1").click(function () {
    window.open('https://www.linkdesktop.com');
});

}

});

2 个答案:

答案 0 :(得分:1)

在我的上一个项目中,我使用了这个solution to check mobile user。优雅而简单。

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() ) alert('Mobile');

答案 1 :(得分:0)

您可以检查用户代理(window.navigator.userAgent),参见Determine if user navigated from mobile Safari