问题
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