最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Easiest way in browser to detect mobile phones - Stack Overflow

programmeradmin3浏览0评论

I use a script generator -- / -- to detect mobile phones in order to redirect the page to a subdomain that has a special template for phones.

I'm only interested in mobile phones only. Ipad, Android tablets will not be redirected. All I need to know is if this script covers IPhone 4 and IPhone 5, because I don't have these models to test it. Anyway, I have tested on Safari using Developer menu - user agent - Safari ios 4.3.3 Iphone, and the page is redirected as needed. Is this enough for what I want or I should use the following script too:

var iphone4 = (window.screen.height == (960 / 2)) ? true : false;
var iphone5 = (window.screen.height == (1136 / 2)) ? true : false;
if (iphone4 && iphone5) {
    parent.location.href='';
}

I use a script generator -- http://detectmobilebrowsers./ -- to detect mobile phones in order to redirect the page to a subdomain that has a special template for phones.

I'm only interested in mobile phones only. Ipad, Android tablets will not be redirected. All I need to know is if this script covers IPhone 4 and IPhone 5, because I don't have these models to test it. Anyway, I have tested on Safari using Developer menu - user agent - Safari ios 4.3.3 Iphone, and the page is redirected as needed. Is this enough for what I want or I should use the following script too:

var iphone4 = (window.screen.height == (960 / 2)) ? true : false;
var iphone5 = (window.screen.height == (1136 / 2)) ? true : false;
if (iphone4 && iphone5) {
    parent.location.href='http://www.mobile.mysite.';
}
Share Improve this question edited Mar 27, 2013 at 20:17 Blazer asked Mar 27, 2013 at 19:52 BlazerBlazer 3164 silver badges16 bronze badges 5
  • I would say that screensize is a safer way to do this if you are only interested in screen resolutions. If you have mobile specific features at the subdomain, then this sort of hacky thing is the only method I am aware of. – thatidiotguy Commented Mar 27, 2013 at 20:00
  • I would use Mobile_Detect PHP Class. It has all kinds of options, and can even add your own detection parameters. Source: code.google./p/php-mobile-detect – adamdehaven Commented Mar 27, 2013 at 20:01
  • There's more and more overlap in screen sizes between phones and tablets these days. – Matt Coughlin Commented Mar 27, 2013 at 20:08
  • I thought the same to, otherwise I would use a simple script like: if( screen.width <= 500) {location.href="reditect.html"} – Blazer Commented Mar 27, 2013 at 20:12
  • Adam D, my site is HTML, so I can't use Mobile_Detect PHP. – Blazer Commented Mar 27, 2013 at 20:22
Add a ment  | 

2 Answers 2

Reset to default 5

Yes, the regex from http://detectmobilebrowsers./ will detect iPhone (and iPod Touch for that matter) all versions...ip(hone|od) is the regex portion that will match it.

if you wanted a script just for iPhone/iPod you could trim the aforementioned script down to:

(function (a, b) { if (/ip(hone|od)/i.test(a)) window.location = b }
)(navigator.userAgent || navigator.vendor || window.opera, 'http://www.mobile.mysite.');

EDIT:

or instead use this script to detect Mobile devices

(function (a, b) { if (/Mobi/.test(a)) window.location = b }
)(navigator.userAgent || navigator.vendor || window.opera, 'http://www.mobile.mysite.');

based on the remendation of Browser detection using the user agent | MDN:

we remend looking for the string "Mobi" anywhere in the User Agent to detect a mobile device. If the device is large enough that it's not marked with "Mobi", you should serve your desktop site (which, as a best practice, should support touch input anyway, as more desktop machines are appearing with touchscreens).

This should work.

var bIsMobile = (navigator.userAgent.toLowerCase().indexOf("mobile") != -1 && navigator.userAgent.toLowerCase().indexOf("ipad") == -1);
发布评论

评论列表(0)

  1. 暂无评论