only load the whole jquery stuff if a javascript condition is fulfilled

余生长醉 提交于 2019-12-12 04:57:37

问题


I would like to check, if there are more than tho large images on my php-generated forum-page, and only if so, then I would like to load jquery and do some jquerystuff.

How can I dynamically load the library only if the condition succeeds?


回答1:


(function() {
  if (something) {
    var s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js';
    (document.getElementsByTagName('head')[0] ||
      document.getElementsByTagName('body')[0]).appendChild(s);
  }
}());


来源:https://stackoverflow.com/questions/16756908/only-load-the-whole-jquery-stuff-if-a-javascript-condition-is-fulfilled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!