Take the 2-minute tour ×

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">

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

share|improve this question
You Could Take a Look at the Useragent –  C5H8NNaO4 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, 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 Answer 1

up vote 3 down vote accepted

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

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


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

share|improve this answer

Your Answer


