3

This question may have been asked before, but I couldn't find an answer to it using both Google and the built-in search function of this site.

I want to use jQuery on my website, but only if it is displayed on a desktop computer. If my site is displayed on a mobile device, I don't want it to load jQuery.

What's the best way to do this? Should I use Javascript or PHP for detecting the device type?

snorpey
  • 2,327
  • 3
  • 18
  • 28

3 Answers3

3

http://jsfiddle.net/fk9kC/

The code above is pretty much self-explanatory - by using a DOM element being hidden or shown depending by a screen width you're being able to target mobile phones more or less accurately.

Furthermore, you can also replace the device-width statement with max-device-width.

ienes
  • 256
  • 1
  • 2
  • 16
1

What about this? Auto detect mobile browser (via user-agent?)

Community
  • 1
  • 1
dmitko
  • 2,607
  • 2
  • 21
  • 15
1

You would need to check the user agent.

You will need to define what is a mobile platform to you.

One hack may be to include a media type mobile stylesheet, and have it set an element to something specific that you could check for using JavaScript. This is hacky though, but it will mean you won't need to parse user agents.

alex
  • 479,566
  • 201
  • 878
  • 984
  • I think I'm going to try your solution, since a _mobile platform_ to me is defined by the screen size, not the user agent. Do you have examples of this technique by any chance? – snorpey Sep 16 '10 at 11:27
  • @snorpey I was going to code something up, but it looks like [ienes](http://stackoverflow.com/questions/3725970/dont-load-jquery-on-mobile-devices/3726795#3726795) has done it already. – alex Sep 16 '10 at 13:59