Allow access only from mobile and tablet devices

纵然是瞬间 提交于 2019-12-08 11:59:02

问题


My client has very specific request. He wants to block all "classic" computers (desktop/notebook).

So if I access the website from iPhone, Android or tablet it displays everything (the different resolutions I can hadle via responsive design). But when somebody from Mac or Windows or Linux (I know that Android is also based on gnu/linux ;) ) access the web, he gets only a message that "this web is only for mobile devices"or something like taht.

But I am not sure how to "ban" mac os, win, linux etc.

Could javascript(jQuery) library e.g. Modernizr or other do such specific condition "just mobile devices"?

What about Windows 8 :( ?

i am really thankful for any advice, because I have never had such request before?


回答1:


I think you could do this in several ways.

Either you check via javascript, but then you if somebody would turn off javascript you would be screwed again because they could take a look at the website:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
 // some code..
}

or you could check it via modernizr, but I don't know exactly how to do it in modernizr and you would also have the problem of turned off javascript code,

or you could download a script that is suitable for your case via:

http://detectmobilebrowsers.com/

another thing you could do is checking the screen resolution but, then we are back at the javascript case.




回答2:


According to MSDN, Windows adds Tablet PC to the browser user agent if it is a tablet.

I suggest not doing this via javascript, because as states above: if javascript is disabled, people will be able to visit your site.



来源:https://stackoverflow.com/questions/13247563/allow-access-only-from-mobile-and-tablet-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!