仅将特定URL重定向到移动设备上的其他URL

时间:2014-10-24 18:01:04

标签: javascript php html mobile responsive-design

我建立了自适应网站,主页在移动设备上是多余的,我希望能够重定向到特定的内部网页。我找到了重定向到移动版网站的解决方案,但这不起作用,因为我得到了重定向循环,或者您无法访问该网站上的任何其他页面。

例如我想重定向: http://www.url.com/home

http://www.url.com/portfolio

仅限移动设备。只有那一页。

3 个答案:

答案 0 :(得分:1)

将此代码插入http://www.url.com/home文件

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) || navigator.userAgent.match(/WPDesktop/i);
},
any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}};
if( isMobile.any() ) {
    window.location.assign("http://www.url.com/portfolio");
}

答案 1 :(得分:1)

这可以通过使用一些JavaScript轻松实现。 if语句中的代码检测用户是否正在使用移动设备(Android,iOS,Kindle,BlackBerry,Windows Phone,Kindle等),if语句中的代码只是将用户重定向到您指定的链接。 / p>

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) {
    window.location.replace("http://www.url.com/portfolio");
}

答案 2 :(得分:0)

如果您使用的是php,那么有些库可以进行移动检测。 Google实际上提供了一个:https://code.google.com/p/php-mobile-detect/

在将任何内容发送给客户端之前,将移动检测设置位置设置为http://www.url.com/portfolio

如果它是javascript它有点棘手,但你可以大致了解用户代理并将变量设置为什么类型的设备。

What is the best way to detect a mobile device in jQuery?

获得该变量后,您可以对重定向网址执行document.location。