Is there any way I can restrict the execution (if statement) of javascript so that it only applies to desktop puters, 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.
Is there any way I can restrict the execution (if statement) of javascript so that it only applies to desktop puters, 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.
- 2 You Could Take a Look at the Useragent – Moritz Roessler Commented Dec 23, 2012 at 18:40
- stackoverflow./questions/4117555/… – adeneo Commented Dec 23, 2012 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 plex graphical things. Isn't this rather an issue of display resolution? – Pekka Commented Dec 23, 2012 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 Commented Dec 23, 2012 at 18:49
- the parallax doesn't work properly on any device apart from desktop – Muhammed Bhikha Commented Dec 23, 2012 at 18:53
1 Answer
Reset to default 6One 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 remend using feature detection instead, try looking into: Modernizr