Pepijn Gieles
Pepijn Gieles

Reputation: 727

Load jQuery file if window width is between values

I found a lot of solutions but got none of them working..

What I have is this:

$(document).ready(function(){
    if ( $(window).width() > 480) {

    }
});

What I want to know is how can I load an external js file into my html head when the window width is greater than 480px but smaller than 768px. I tried with 'load' and 'write' but got none of it working.

Thanks in advance!

Upvotes: 1

Views: 9831

Answers (2)

Harshal Solanki
Harshal Solanki

Reputation: 21

// I loaded value between 901 to 1600 width, You can run with your min-width or max-width
jQuery(document).ready(function($){ // Or else use with your requirment function (ex.$(window).on('load') or click function etc.)
var windowsize = $(window).width();
if ((windowsize >= 901) && (windowsize <= 1600)) {
       //Do Your Code Here... 
}
});

Upvotes: 0

Alex
Alex

Reputation: 10216

Use getScript

$(document).ready(function(){
    if ( $(window).width() > 480 && $(window).width() < 768) {
        $.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
          console.log( data ); // Data returned
          console.log( textStatus ); // Success
          console.log( jqxhr.status ); // 200
          console.log( "Load was performed." );
        });
    }
});

Upvotes: 3

Related Questions