javascript 如何检测页面请求是来自移动客户端还是桌面客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8760143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect a page request is from mobile or desktop client side
提问by user1134567
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。
回答by ronme
http://detectmobilebrowsers.com/has an open source javascript function to detect a mobile browser that you can use.
http://detectmobilebrowsers.com/有一个开源的 javascript 功能来检测您可以使用的移动浏览器。
回答by m.edmondson
To determine this you need to check the HTTP header for the request for your website, in particular the User-Agentstring.
要确定这一点,您需要检查网站请求的 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 服务器级别或在您的应用程序内重定向。
回答by tkone
Don't make separate sites. Use reactive design. The browser on my phone is just as good as the one on my desktop.
不要制作单独的网站。使用反应式设计。我手机上的浏览器和我桌面上的浏览器一样好。
回答by Basavaraj Hadimani
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 :)
快乐学习:)