Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code to detect the users device.

 
Mark Peterson
Greenhorn
Posts: 5
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard that it's possible to detect a user's device via JavaScript code. I need to add this code to a website so that either the full website or the mobile website displays when the user accesses it -- i.e.: desktop computer, smartphone, laptop, tablet, etc. Can you give me an example of how this might be accomplished?

Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the user agent to get a rough idea. This is the browser not the device though so an iPhone and iPad will look the same. You can also get the screen resolution. The problem is that a tablet and same laptop can have the same resolution. I don't know of a universal way.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you need to choose between two fundamentally different sets of views*, most designers would choose responsive design over browser sniffing.


* A web app I support needs this as the mobile and desktops views are radically different.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just finished reading Head First Mobile Web (a good book). [Chapter 5]

You can do detection on the server side using mobile device data sources (some free, some not free).

Look into stuff like WURFL.

You'll get tons of information about the browser, platform, hardware features of the devices, etc.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesus Angeles wrote:Look into stuff like WURFL.

That's what this site uses to determine whether to show the mobile view. I didn't answer with that earlier since it isn't done in JavaScrip.t
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if doing an ajax call will work on this.

(the ajax call will call a server code that returns the info needed [using wurfl, etc.])
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesus Angeles wrote:I wonder if doing an ajax call will work on this.

(the ajax call will call a server code that returns the info needed [using wurfl, etc.])

That's a good idea! It should as AJAX is coming form the client.
 
Rob Crowther
author
Ranch Hand
Posts: 297
5
Android Fedora Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's more efficient to write some sort of data into your page (eg. a JSON object) on the initial request. All the information Wurfl would use from the Ajax request is already available in the initial request, so why make an extra HTTP request to get the data? Also beware the licensing on Wurfl changed last year (controversially) so you now have to buy a license to use the database of devices, I recently switched to OpenDDR on the main project at work because of this.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,
I gave you a cow for pointing out that helpful fact!

This site is still on version 2 so we are ok for now. But i added switching to our list of things to do
 
Rob Crowther
author
Ranch Hand
Posts: 297
5
Android Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne!
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, What is your experience with using OpenDDR so far?

[I am going to use it (to get the width of the device; in physical length like millimeter) for total control (while still being fluid and complies to RWD) of the layout served (along with letting user override default layout).]

[edit] It looks like the physical width is available in WURFL (not sure how reliable), but not in OpenDDR, nor in 51Degrees.

[edit] Ill give in for now and not use server detection. (Hoping I wont need it till DeviceMap hopefully completes and is free).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic