如何检测用户使用的是手机,平板电脑还是台式机并重定向它们? [英] How to detect whether the user is using mobile, tablet or desktop and redirect them?

查看:87
本文介绍了如何检测用户使用的是手机,平板电脑还是台式机并重定向它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道一个响应性网站不是. 1,但不幸的是目前还没有选择!我也有很强的javascript编码技能.

First off I know that a responsive site is no. 1 but at the time being that unfortunately is not an option! I also have very javascript coding skills.

我需要检测用户使用的是台式机,平板电脑还是移动设备,然后将其重定向.

I need to detect whether the user is using a desktop, a tablet or a mobile and redirect them.

用户使用桌面-留在站点(A). 用户使用平板电脑-从A重定向到网站B. 用户使用移动设备-从A重定向到站点C.

User uses a desktop - stay on site (A). User uses a tablet - redirect from A to site B. User uses a mobile - redirect from A to site C.

如果我的屏幕宽度和dpi是否仍然有可能被某些手机和平板电脑捕获并重定向到台式机版本?

If I screen width and dpi is there not a possibility that some mobiles and tablets gets caught anyway and redirect to desktop version?

我现在有这个.

<script>
if (navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
) {
document.location = "B";
} else if (navigator.userAgent.match(/iPad/i)
) {
document.location = "C";
} else {
document.location = "A";
}
</script>

预先感谢您的帮助!

推荐答案

如果有人感兴趣,我想出了这个解决方案.到目前为止,效果很好.不知道将来是否可以安全使用-可能不是.但是,如果您没有其他解决方案,或者无法建立一个令人反感的网站或类似的网站,则可以适当地使用此...

If anyone is interested I came up with this solution. So far it works great. Don't know if it is safe to use in the future - probaply not. But if you have no other solution or not able to great a repsonsive website or something like that you can propably use this...

我使用最常见的设备.

if (/iP(hone|od)|android.+mobile|BlackBerry|IEMobile/i.test(navigator.userAgent)) {
    window.location.replace("http://mobile");
} else if (/(tablet|ipad|playbook|silk)|(android(?!.*mobile))/i.test(navigator.userAgent)) {
    window.location.replace("http://tablet.website.com");
}

这篇关于如何检测用户使用的是手机,平板电脑还是台式机并重定向它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆