简体   繁体   English

如何检测页面请求来自移动或桌面客户端

[英]How to detect a page request is from mobile or desktop client side

We have developed a website which has general homepage and mobile page, one is for mobile and other is for PC. 我们开发了一个网站,它有一般的主页和移动页面,一个用于移动,另一个用于PC。

I want code to detect the device automatically and navigate to the mobile homepage if the user is from mobile and navigate to the homepage(general) if the user is from PC and problem is it should be using JavaScript or plain HTML. 我想要代码自动检测设备并导航到移动主页如果用户来自移动设备并导航到主页(一般)如果用户来自PC并且问题是应该使用JavaScript或纯HTML。

http://detectmobilebrowsers.com/有一个开源的javascript函数来检测你可以使用的移动浏览器。

To determine this you need to check the HTTP header for the request for your website, in particular the User-Agent string. 要确定这一点,您需要检查网站请求的HTTP标头,特别是User-Agent字符串。

You haven't mentioned what technologies you're using, but you can redirect at the web-server level or within your application. 您还没有提到您正在使用的技术,但您可以在Web服务器级别或应用程序中重定向。

Don't make separate sites. 不要制作单独的网站。 Use reactive design. 使用反应式设计。 The browser on my phone is just as good as the one on my desktop. 我手机上的浏览器和我桌面上的浏览器一样好。

you can use below code, 你可以使用下面的代码,

var index = navigator.appVersion.indexOf("Mobile");

This will return you negative value if the request is not from mobile site. 如果请求不是来自移动网站,则会返回负值。

Happy learning :) 快乐学习:)

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

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