由于加载jQuery时执行/不执行脚本 – 为什么?

这是我遇到的问题。 试试这段代码 :

if (typeof jQuery == 'undefined') { function getScript(url, success) { var script = document.createElement('script'); script.src = url; var head = document.getElementsByTagName('head')[0]; done = false; script.onload = script.onreadystatechange = function () { if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { done = true; success(); script.onload = script.onreadystatechange = null; head.removeChild(script); }; }; head.appendChild(script); }; getScript('http://code.jquery.com/jquery-1.11.2.min.js', function () { if (typeof jQuery !== 'undefined') { jQuery(document).ready(function ($) { MyFunction($); }); } }); } else { jQuery(document).ready(function ($) { MyFunction($); }); } function MyFunction($) { $.getJSON("http://archiesocial.progettiarchimede.it/widget_privacy/test.aspx?asd=1&callback=?", function (d) { }).done(function(d) { JsonToHtml(d); }); } function JsonToHtml(html) { var items = []; $.each(html, function (key, val) { items.push(val); }); $('body').prepend(items.join('')); } 

你会注意到我的代码检查是否加载了jQuery。 如果没有,它从外部源加载一个版本; 而不是检索JSON,解析它并“执行它”。

正如你所看到的,在主体内部加载的脚本根本没有加载( 这是我的问题 )。

现在,尝试在小提琴中选择jQuery的版本/库(1.8.3就可以了)并按下播放:您将看到脚本/按钮渲染:脚本被执行!!!

为什么首先加载jQuery(这里)渲染脚本,然后加载jQuery不会执行脚本? 你能帮助我吗?

我认为最好的办法是强制onload事件被激活,因为当你加载jQuery(如果未定义)时,这个事件已经被触发了。 这是一种解决方法:

 function JsonToHtml(html) { var items = []; $.each(html, function (key, val) { items.push(val); }); $('body').prepend(items.join('')); if (document.readyState === 'complete') { // check if document is complete var evt = document.createEvent('Event'); evt.initEvent('load', false, false); window.dispatchEvent(evt); // then redispatch onload event } } 

-DEMO-

我认为问题在于范围。 函数MyFunction()和JsonToHtml()不在范围内。 (记住你正在使用像getJSON这样的异步函数)也许我的解释是错误的,但是代码可以工作。 :P

使用此代码,您没有问题。

 function _test(){} _test.prototype = { hasjQuery: function(){ if(typeof window.jQuery !='undefined' && !_test.prototype.otherLibrary() ) { return true; }else{ return false; } }, otherLibrary: function(){ if (typeof document.$ == 'function') { return true; }else{ return false; } }, inyectjQuery: function(url, success){ var script = document.createElement('script'); script.src = url; script.id = "delete"; done = false; script.onload = script.onreadystatechange = function() { if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { done = true; success(); script.onload = script.onreadystatechange = null } }; document.getElementsByTagName('head')[0].appendChild(script) }, myFunction: function(){ urljQuery = 'http://code.jquery.com/jquery-latest.min.js'; if(_test.prototype.hasjQuery()){ jQuery.getJSON("http://archiesocial.progettiarchimede.it/widget_privacy/test.aspx?asd=1&callback=?", function (d) { _test.prototype.JsonToHtml(d); }).done(function() { console.log("Success getJSON action"); }); }else{ _test.prototype.inyectjQuery(urljQuery, function(){ if (typeof window.jQuery == 'undefined') { console.log("unable to load jQuery"); }else{ jQuery.getJSON("http://archiesocial.progettiarchimede.it/widget_privacy/test.aspx?asd=1&callback=?", function (d) { _test.prototype.JsonToHtml(d); }).done(function() { console.log("Success getJSON action"); }); } }); } }, JsonToHtml: function(html){ var items = []; jQuery.each(html, function (key, val) { items.push(val); }); jQuery('body').prepend(items.join('')); } } test = new _test(); test.myFunction();