user3462744
user3462744

Reputation: 191

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.

Upvotes: 14

Views: 24416

Answers (2)

aelor
aelor

Reputation: 11116

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.

Upvotes: 4

Pratik Joshi
Pratik Joshi

Reputation: 11693

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.

Upvotes: 25

Related Questions