Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript How to run a jquery function but only when the browser window width is > 1024px? Re: How to run a jquery function but only when the browser window width is > 1024px?

#116138
JoniGiuro
Participant

you could do:

$(document).ready(function() {
var pageWidth = $(window).width();
var body= document.getElementsByTagName(‘body’)[0];
var script= document.createElement(‘script’);
script.type= ‘text/javascript’;
if (pageWidth > 1024) {
script.src= ‘desktop.js’;
}else{
script.src= ‘mobile.js’;
};
body.appendChild(script);
});

that way you only load the needed script (it makes sense if the scripts are big in filesize)