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?
December 3, 2012 at 3:58 am
#116138
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)