简体   繁体   English

移动设备检测问题

[英]Mobile device detection issue

Today I added code to my websites head section to detect mobile device via screen size, code is: 今天,我将代码添加到我的网站头部,以通过屏幕尺寸检测移动设备,代码为:

<script type="text/javascript">
if (screen.width <= 699) {document.location = "http://m.mywebsite.com";}
</script>

It works great for me, but the problem now is when I am redirected to mobile version of website, I got there link to go to desktop version of site. 它对我来说很棒,但是现在的问题是,当我重定向到网站的移动版本时,我到达那里的链接可以转到网站的桌面版本。 When I hit it, I am redirected to desktop version and then immediately redirected back to mobile site, for the reason I have mobile website detection! 当我点击它时,我被重定向到桌面版本,然后立即重定向回移动网站,原因是我可以检测到移动网站! :-) it is endless circle. :-)这是无尽的循环。 How to fix this problem? 如何解决这个问题?

Size of the screen is probably not the best way to detect a mobile device. 屏幕尺寸可能不是检测移动设备的最佳方法。 Take a look at http://wurfl.io/ and see if that is useful. 看一下http://wurfl.io/ ,看看是否有用。 And yes, you need a cookie or some local storage to avoid the looping 是的,您需要一个cookie或一些本地存储来避免循环

Relying on screen size alone is not enough. 仅仅依靠屏幕尺寸是不够的。 In fact many modern smartphones have screens of well over 800 pixels wide. 实际上,许多现代智能手机的屏幕宽度都超过800像素。 And then you also have tablets, smart TVs, consoles and e-readers. 然后,您还拥有平板电脑,智能电视,控制台和电子阅读器。 The best thing to do is use an existing solution like 51Degrees.com . 最好的办法是使用现有的解决方案,例如51Degrees.com

It's easy to set up and the database is updated with new devices on regular basis. 它很容易设置,并且定期使用新设备更新数据库。 Detection is done on your server, which improves page-loading times for your users. 检测是在您的服务器上完成的,这可以缩短用户的页面加载时间。 Additionally you can use the image optimiser to handle image resizing when you redirect to mobile page. 另外,当您重定向到移动页面时,可以使用图像优化器来处理图像大小调整。

For an easy, 4-step setup of PHP detector visit: PHP Getting Started . 要轻松进行4步的PHP检测器设置,请访问: PHP入门

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM