Adam
Adam

Reputation: 1459

Only load javascript file on wide screen size?

I would like to know how I can load a javaScript file only when the screen size is wider than 1000px.

If I was in the javaScript file, and wanted to do something, I would simply do this...

function example(){
  if ($(window).width() > 1000) {
    //do something here....
  }
}

...however I want to call the whole JS file when wide enough.

So the logic will be something like this at the bottom of my HTML file...

If page is wide enough...
   <script src="path/to/file"></script>

Any suggestions would be appreciated.

Upvotes: 1

Views: 474

Answers (1)

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

You could use $.getScript, like so:

$.getScript('script.js', function(){  
  // here the code in script.js is available
});

and for the resize

$(window).resize(function() {});

Upvotes: 2

Related Questions