简体   繁体   English

如何检测用户使用的是手机,平板电脑还是台式机并重定向它们?

[英]How to detect whether the user is using mobile, tablet or desktop and redirect them?

First off I know that a responsive site is no. 首先,我知道响应式网站不是。 1 but at the time being that unfortunately is not an option! 1,但不幸的是目前还没有选择! I also have very javascript coding skills. 我也有很好的javascript编码技能。

I need to detect whether the user is using a desktop, a tablet or a mobile and redirect them. 我需要检测用户是否正在使用台式机,平板电脑或移动设备,然后将其重定向。

User uses a desktop - stay on site (A). 用户使用桌面-留在站点(A)。 User uses a tablet - redirect from A to site B. User uses a mobile - redirect from A to site C. 用户使用平板电脑-从A重定向到站点B。用户使用平板电脑-从A重定向到站点C。

If I screen width and dpi is there not a possibility that some mobiles and tablets gets caught anyway and redirect to desktop version? 如果我的屏幕宽度和dpi是否有可能被某些手机和平板电脑捕获并重定向到台式机版本?

I have this at the moment. 我现在有这个。

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

Thank you in advance for your help! 预先感谢您的帮助!

You can easily do this by showing different content with loading diff. 您可以通过加载diff显示不同的内容轻松地做到这一点。 css, if not wanting to go fully responsive. CSS,如果不想完全响应。 I would use @media tag of CSS, like in W3School dummy example 我会使用CSS的@media标签,就像在W3School虚拟示例中一样

@media screen and (min-width: 480px) {
    body {
        background-color: lightgreen;
    }
}

Now, as for your question, with setting the max screen size one value for phones and tablets, you usually won't have problems width catching desktops/laptops. 现在,对于您的问题,通过将手机和平板电脑的最大屏幕尺寸设置为一个值,通常在捕获台式机/笔记本电脑时就不会遇到宽度问题。 With exclusion of new iPad Pro, 99% of tablets are 10'' and less, while phones are 6'' and less 排除新的iPad Pro,99%的平板电脑为10英寸及以下,而手机为6英寸及以下

Personally, I would not do it your way with js, as it adds complexity where it is not needed. 就个人而言,我不会用js来做到这一点,因为它在不需要的地方增加了复杂性。 But this Blog post might be of help as well. 但是此Blog帖子也可能有帮助。

Your solution looks good just add a check for screen width as well which is 970px for ipad or tabs, 768px for mobile devices(smartphones) and greater than 970px for desktop version. 您的解决方案看起来不错,只需添加屏幕宽度检查即可:ipad或标签页为970px,移动设备(智能手机)为768px,台式机版本为970px以上。

As it seems you cannot use media queries as you want to redirect based on devices else media queries are the best way to do responsive design. 似乎您无法使用媒体查询,因为您想基于设备进行重定向,否则媒体查询是进行响应式设计的最佳方法。

You can have a look at below link as well. 您也可以查看下面的链接。

https://css-tricks.com/snippets/javascript/redirect-mobile-devices/ https://css-tricks.com/snippets/javascript/redirect-mobile-devices/

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... 但是,如果您没有其他解决方案,或者无法建立一个令人反感的网站或类似的网站,则可以适当地使用它...

I use the most common devices. 我使用最常见的设备。

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

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

相关问题 检测平板电脑或桌面用户 - Detect tablet or desktop user 检测移动和平板设备并将其重定向到另一个URL的最佳方法? - Best way to detect mobile and tablet devices and redirect them to another url? 如何检测用户是否正在使用Windows平板电脑 - how to detect if user is using windows tablet Impact如何知道设备是台式机,手机,平板电脑等(任何设备)? - How does Impact know whether a device is a desktop, a mobile phone, a tablet, etc (Any Device)? 检测移动用户和桌面是否相同 - Detect if mobile user and desktop is same 如何检测桌面和移动Chrome用户代理? - How do you detect between a Desktop and Mobile Chrome User Agent? 如何使用桌面中的javascript检测用户是否在底部 - how to detect user is in bottom using javascript in desktop 如何检测salesforce lightning应用程序是在移动浏览器还是台式机浏览器上运行? - How to detect whether salesforce lightning app is running on a mobile browser or desktop browser? 使用Javascript,如何检测浏览器是否在平板电脑设备上与手机上运行? - Using Javascript, how can I detect whether the browser is running on a tablet device vs a phone? 如何使用树枝检测屏幕大小或移动/桌面 - how to detect screen size or mobile/desktop using twig
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM