如果窗口宽度在值之间,则加载jQuery文件

问题描述 投票:0回答:2

我找到了很多解决方案,但没有一个起作用。.

我有这个:

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

    }
});

我想知道的是,当窗口宽度大于480px但小于768px时,如何将外部js文件加载到html头中。我尝试使用“加载”和“写入”,但没有一个起作用。

提前感谢!

jquery file load window width
2个回答
1
投票

使用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." );
        });
    }
});

0
投票
// 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... 
}
});
© www.soinside.com 2019 - 2024. All rights reserved.