0

Is there away to know if im running on a desktop or mobile device.

Since desktop responsiveness are diffrent i would like to know if a user can rotate te screen or not.

Nati Krisi
  • 1,023
  • 1
  • 12
  • 23

5 Answers5

0

You can use a script to find your browsers user agent string. Each browser has a different user agent. Safari on Mac and Safari on an iPhone both also have different user agents.

I'm not exactly sure what you mean though, do you want like javascript to know what type of device your on?

Ilan Kleiman
  • 1,149
  • 5
  • 18
  • 36
0

I think you could use modernizr to detect the browser behavior.

Touch Events touch

The Modernizr.touch test only indicates if the browser supports touch events, which does not necessarily reflect a touchscreen device. For example, Palm Pre / WebOS (touch) phones do not support touch events and thus fail this test. Additionally, Chrome (desktop) used to lie about its support on this, but that has since been rectified. Modernizr also tests for Multitouch Support via a media query, which is how Firefox 4 exposes that for Windows 7 tablets. For more info, see the Modernizr touch tests.

It's recommended to set both touch and mouse events together, to cater for hybrid devices – see the Touch And Mouse HTML5 Rocks article.

Lifecube
  • 1,198
  • 8
  • 11
0

The following will give you the user agent.

navigator.userAgent

You can then use something like:

if(navigator.userAgent.indexOf("DESIRED USER AGENT") != -1) {
    // Mobile specific code
}

You should find the following thread helpful: Auto detect mobile browser (via user-agent?)

Community
  • 1
  • 1
keslert
  • 311
  • 1
  • 8
0

You can use Modernizr:

Modernizr creates an element, sets a specific style instruction on that element and then immediately tries to retrieve that setting. Web browsers that understand the instruction will return something sensible; browsers that don't understand it will return nothing or "undefined". Modernizr uses the result to assess whether that feature is supported by the web browser.

Eg:

if ( Modernizr.touch ) {
// mobile
} else {
// desktop
}

The following link has some javascript built-in functions to detect browsers :

http://www.quirksmode.org/js/detect.html

Joke_Sense10
  • 5,341
  • 2
  • 18
  • 22
0

check out mobile detect. it may be a bit much for what you want but it is comprehensive.

gwillie
  • 1,893
  • 1
  • 12
  • 14