Open .js file when MediaQuery is larger than 1024px

1

At the end of index.php (responsive layout) you have a call to

< script type='text/javascript' src='desk.js'>< /script>

It turns out that this JS will only act with MediaQuery greater than 1024px.

I ask: Is it possible to make an IF condition in Java or in the CSS itself that opens this JS?

JS is heavy and mobile has no function at all.

    
asked by anonymous 06.07.2017 / 07:00

1 answer

1

You can get the size of the screen by window.screen.width or window.innerWidth and if it is smaller you can add the js file. ex:

function desktop(){
       if(window.screen.width >= 1024){
           var sc = document.createElement('script')
           sc.src = "desk.js"
           document.querySelector('body').append(sc)
       }
 }

Reference

    
06.07.2017 / 10:02