3 Replies - 2825 Views - Last Post: 17 July 2012 - 07:37 AM

#1 Nouf-j  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-July 12

Help to detect smartphone or tablet !

Posted 17 July 2012 - 04:22 AM

Hi
How i can detect if the user is accessing my website from a smartphone or from a tablet ?
i found this code in the internet .. i couldn't understand it and it doesn't work
any other suggestions please ?

this is the condition from that i found
if(/android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|meego.+mobile|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) 


Is This A Good Question/Topic? 0
  • +

Replies To: Help to detect smartphone or tablet !

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Help to detect smartphone or tablet !

Posted 17 July 2012 - 04:28 AM

View PostNouf-j, on 17 July 2012 - 01:22 PM, said:

i found this code in the internet .. i couldn't understand it and it doesn't work

well, it is supposed to be a RegExp that works on a not further defined string. probably just somme lengthy browser sniffing on the UA-string.

technically there is no way to directly get the device that is used. all that is accessible is info about the used browser that may contain data about the used system. but that does not prevent a user from sending a fake UA-string at all.
Was This Post Helpful? 0
  • +
  • -

#3 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 828
  • Joined: 25-October 08

Re: Help to detect smartphone or tablet !

Posted 17 July 2012 - 07:27 AM

Best thing I have found is to test the size of the screen
but even that is dicey when the orientation is changed.

Phone screen size is "usually": (from: http://mobiforge.com...e-screen-sizes)

The iPhone is 320 pixels wide by 480 pixels high.
Many Nokia N-Series devices are 240 pixels wide by 320 pixels high.
Newer devices often support a landscape mode where the width and height are spontaneously reversed.
Older (yet still popular) Nokia devices have displays ranging from 176 by 208 pixels up to 352 by 416 pixels.
Blackberry screen resolutions range anywhere from 160 x 160 pixels all the way up to 324 x 352 pixels.



Tablet screen size is "usually": (from: http://developer.and...s_support.html)

xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp


Desktop screen size is "usually": bigger

An interesting article about HTML5 testing can be found at:
http://www.texaswebd...link.asp?id=145


But right now it appears to be a big crap shoot as to what would work best for you.
Google for more possible suggestions.
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Help to detect smartphone or tablet !

Posted 17 July 2012 - 07:37 AM

This is normally not the thing that you want to be doing, so what is it that you want to be doing exactly?

If it's sniffing to show different designs of your site based on different widths, don't bother. Use the css @media query based on device width to show alternate content, I believe people are dubbing this as "responsive design" now like it's something new, but it's been around for years (It's the "AJAX" of 2012 :)).

If you're sniffing to show whether a user is online on your site via a mobile device, then it's not mission critical and you should do something that works in 80% of cases, ie. sniff for Android, iPhone and Windows Phone OS and BlackBerry (and RIM Tablet for the PlayBook).

This post has been edited by RudiVisser: 17 July 2012 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1