Jquery:当屏幕尺寸较小时添加脚本标记

时间:2016-01-22 09:15:18

标签: jquery

我试图在屏幕尺寸小于900时添加脚本,但它没有添加任何脚本,但是当屏幕尺寸小于900时我收到警告消息。我的问题与此不同{ {3}}。我的问题是,以下脚本在我的系统中不起作用。

<script>

if($(window).width() <= 900) {
  alert("size is less than 900");
  var sc = document.createElement('script');
  sc.src = 'js-min/bootstrap/jquery.min.js';
  sc.type = 'text/javascript';
  if(typeof sc['async'] !== 'undefined') {
    sc.async = true;
  }  
  document.getElementsByTagName('head')[0].appendChild(sc);
}

</script>

1 个答案:

答案 0 :(得分:0)

为此,请尝试document.head.appendChild()事件。

if($(window).width() <= 900) {
  alert("size is less than 900");
  var sc = document.createElement("script");
  sc.src = "js-min/bootstrap/jquery.min.js";
  sc.type = 'text/javascript';
  if(typeof sc['async'] !== 'undefined') {
    sc.async = true;
  } 
  document.head.appendChild(sc);
}