簡體   English   中英

如果窗口小於 600px,則使用純 JavaScript 有條件地加載 jquery

[英]Load jquery conditionally with pure JavaScript if windows with < than 600px

如果窗口小於 600px 並放置一個以異步方式加載 jquery 的“else”,有沒有辦法用純 JavaScript 有條件地加載 jquery?

像這樣的東西?

var sync;
if (window.outerWidth < 600) {
  sync = false;
} else {
  sync = true;
}

function loadScript(sync) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.async = sync;
  script.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.js";
  document.getElementsByTagName("head")[0].appendChild(script);
};
loadScript(sync);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM