Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If mobile disable certain scripts

I want to be able disable certain scripts that are in the head section of my page when a mobile device is detected.

At the minute I have this:

$(function(){
    var mobile;
    if (window.width <= 479) {
        //don't load these scripts 
    }
    else {
        //load all scripts
    }
});

I'm struggling to find the code to disable the scripts from running when mobile width is detected. Any help or other ideas would be much appreciated.

like image 533
user3462744 Avatar asked Mar 27 '14 07:03

user3462744


2 Answers

Use following

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 // some code..
}else
{
//Now include js files
}

for your second Question

use

if (window.width > 479) {
 $('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />');
}

here you can include css or js file.

like image 143
Pratik Avatar answered Oct 21 '22 22:10

Pratik


if (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)) {
        // what you want to run in mobile
}

you can do something like this to test if it is a mobile.

if you are only concerned with the width :

if ($(window).width() <= 479) {
     do stuff
}

or you can use this package.

like image 29
aelor Avatar answered Oct 22 '22 00:10

aelor