File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Code to detect the users device. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Code to detect the users device." Watch "Code to detect the users device." New topic
Author

Code to detect the users device.

Mark Peterson
Greenhorn

Joined: Sep 08, 2013
Posts: 3

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2046
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

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

Joined: Feb 26, 2005
Posts: 2046
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

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

Joined: Nov 06, 2012
Posts: 268
    
    4

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.


Read my books: Hello! HTML5 & CSS3 | HTML5 in Action | Read my blog
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

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

Joined: Nov 06, 2012
Posts: 268
    
    4

Thanks Jeanne!
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2046
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code to detect the users device.
 
Similar Threads
Spring Mobile Hello World Example that includes DeviceResolver, SitePreference, urlPath SiteSwitcher
JMF and Java Web Start
JMF and Java Web Start in Window 7
get the device letter of usb as soon as it connected to the computer
JMF MediaLocator()