3

Is there any way I can restrict the execution (if statement) of javascript so that it only applies to desktop computers, It needs to be off when the user is on a mobile/tablet device. Below is the code, however obviously it applies for any device at the moment when the document is ready.

<script type="text/javascript">
$(document).ready(function(){
    main_parallax();
    main_scrolling();
    sauces_slider();
});    
</script>

Only the main_parallax(); function should be omitted when on a mobile/tablet device.

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
Muhammed Bhikha
  • 4,501
  • 8
  • 34
  • 46
  • 2
    You Could Take a Look at the Useragent – Moritz Roessler Dec 23 '12 at 18:40
  • http://stackoverflow.com/questions/4117555/simplest-way-to-detect-a-mobile-device – adeneo Dec 23 '12 at 18:40
  • Why does the effect need to be off on *all* mobile devices? Many tablets are strong enough to do all sorts of complex graphical things. Isn't this rather an issue of display resolution? – Pekka Dec 23 '12 at 18:49
  • You might want to look at [Modernizr](http://modernizr.com), which makes it easy to test for "touch" events. There's no really solid way, no matter what library you use, to detect whether a device is a "mobile" device. – Pointy Dec 23 '12 at 18:49
  • the parallax doesn't work properly on any device apart from desktop – Muhammed Bhikha Dec 23 '12 at 18:53

1 Answers1

6

One method is using the user agent, which you could do like this:

$(document).ready(function(){
    if (/Android|BlackBerry|iPhone|iPad|iPod|webOS/i.test(navigator.userAgent) === false) {
        main_parallax(); //Run main_parallax() not a mobile device
    }​

    main_scrolling();
    sauces_slider();
});

I would recommend using feature detection instead, try looking into: Modernizr

PhearOfRayne
  • 4,762
  • 3
  • 28
  • 44