简体   繁体   English

jQuery:尝试动态加载 jQuery。 直到脚本运行后才实例化

[英]jQuery: trying to load jQuery dynamically. Not instantiating until after script has been run

javascript:var width=1900;
var height=800;

var jQueryScriptOutputted = false;
function initJQuery() {
    if (typeof(jQuery) == 'undefined') {
        if (! jQueryScriptOutputted) {
            jQueryScriptOutputted = true;
        var oHead = document.getElementsByTagName('HEAD').item(0);
    var oScript= document.createElement("script");
    oScript.type = "text/javascript";
    oScript.src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
    oHead.appendChild( oScript);
        }
        setTimeout("initJQuery()", 50);
    } else {
        $(function() { 
        });
    }

}
initJQuery();



var container=$("#foreplay-root");
var canvas=$("#foreplay-root canvas");
canvas.width(width);
canvas.height(height);
container.width(width);
container.height(height);

the above is a bookmark (hence the javascript: at the beginning).上面是一个书签(因此javascript:在开头)。

but, in the console, it says $ is undefined.但是,在控制台中,它说 $ 未定义。 But when I type $ or jQuery into the console (webkit of course) I get f unction (a,b){return new e.fn.init(a,b,h)} which is correct.但是当我在控制台(当然是 webkit)中输入 $ 或 jQuery 时,我得到了正确unction (a,b){return new e.fn.init(a,b,h)}

So, I'm thinking there is something wrong with the time it takes to init jQuery from teh time it loads.因此,我认为从加载 jQuery 开始所需的时间有问题。

*NOTE: this is for angry birds. *注意:这是针对愤怒的小鸟。 http://chrome.angrybirds.com/ http://chrome.angrybirds.com/

usually to everything that loads a file can be attached an event handler "onload"...通常可以将事件处理程序“onload”附加到加载文件的所有内容......

var jQueryLoaded = false;
function initJQuery() {
    if (typeof(jQuery) == 'undefined') {
        if (! jQueryScriptOutputted) {
            jQueryScriptOutputted = true;
        var oHead = document.getElementsByTagName('HEAD').item(0);
    var oScript= document.createElement("script");
    oScript.type = "text/javascript";
    oScript.src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
    oScript.onload = function() {
       jQueryLoaded = true;
    }
    oHead.appendChild(oScript);
        }
        setTimeout("initJQuery()", 50);
    } else {
        while(!jQueryLoaded) {}
        $(function() { 
        });
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM