等待加载通过javascript添加的脚本
我需要添加jquery,然后添加另一个依赖jquery的脚本。 然后,我需要有使用这两种资产的代码,但我的问题是,在我知道这两种资产都已加载之前,我不希望代码运行 我认为这个过程应该是加载jquery,然后等待window.onload加载jquery,然后加载jquery插件,然后检测插件是否已加载,然后加载我自己使用jquery插件函数的代码 迄今为止的代码:等待加载通过javascript添加的脚本,javascript,jquery,Javascript,Jquery,我需要添加jquery,然后添加另一个依赖jquery的脚本。 然后,我需要有使用这两种资产的代码,但我的问题是,在我知道这两种资产都已加载之前,我不希望代码运行 我认为这个过程应该是加载jquery,然后等待window.onload加载jquery,然后加载jquery插件,然后检测插件是否已加载,然后加载我自己使用jquery插件函数的代码 迄今为止的代码: // load jquery if it is not allready loaded and put it into no con
// load jquery if it is not allready loaded and put it into no conflict mode so the $ is available for other librarys that might be allready on the page.
if(!window.jQuery) {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(script);
jQuery.noConflict(); // stop jquery eating the $
console.log("added jquery");
}
window.onload = function(e) {
// we know that jquery should be available now as the window has loaded
if ( !jQuery.isFunction(jQuery.fn.serializeObject) ) { // use jquery to ask if the plugins function is allready on the page (don't do this if the website already had the plugin)
// website didn't have the plugin so add it to the page.
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery-serialize-object/2.5.0/jquery.serialize-object.min.js";
document.getElementsByTagName('head')[0].appendChild(script);
}
if ( !jQuery.isFunction(jQuery.fn.serializeObject) ) {
// console.log("serializeObject is undefined");
// its going to be undefined here because Its still loading in the script
} else {
// console.log("we have serializeObject");
}
// I now dont know when to call my code that uses .serializeObject() because it could still be loading
// my code
var form_data_object = jQuery('form#mc-embedded-subscribe-form').serializeObject();
};
你必须像这样做
包括
<script type="text/javascript" id="AssetJS"></script>
一个简单的方法是使用headjs。它在几个项目上运行良好。好的,我设法找到了另一种适合我特定需求的方法,所以我回答了我自己的问题。 从中使用此函数 } 在我的例子中的用法:
if(!window.jQuery) {
loadScript("https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js", function () {
jQuery.noConflict(); // stop jquery eating the $
console.log('jquery loaded');
if ( !jQuery.isFunction(jQuery.fn.serializeObject) ) { // use jquery to ask if the plugins function is already on the page
loadScript("https://cdnjs.cloudflare.com/ajax/libs/jquery-serialize-object/2.5.0/jquery.serialize-object.min.js", function () {
console.log('serialize loaded');
SURGE_start(); // both scrips where not on the website but have now been added so lets run my code now.
});
}
});
} else {
if ( !jQuery.isFunction(jQuery.fn.serializeObject) ) { // use jquery to ask if the plugins function is already on the page
loadScript("https://cdnjs.cloudflare.com/ajax/libs/jquery-serialize-object/2.5.0/jquery.serialize-object.min.js", function () {
console.log('serialize loaded');
SURGE_start(); // jquery was on the web page but the plugin was not included. now we have both scripts lets run my code.
});
} else {
SURGE_start(); // web page already had both scripts so just run my code.
}
}
如果jQuery加载:@user2129021请检查我的答案。请。答案是“仅仅是一个指向外部站点的链接”。是否必须在标记中才能使用?我没有访问页面标记的权限。我只能添加为我添加脚本的单个脚本。因此,我需要首先使用尚未具有src属性的javascript向头部添加脚本?您正在使用
var脚本
,仅使用该脚本检查是否加载了jquery序列化对象
,但我需要尽快将jquery置于noConflict模式加载。但是.load()将无法用于检查添加jquery的脚本是否已加载,因为我首先需要使用jquery。load()您可以通过.load()
function loadScript(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function () {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
if(!window.jQuery) {
loadScript("https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js", function () {
jQuery.noConflict(); // stop jquery eating the $
console.log('jquery loaded');
if ( !jQuery.isFunction(jQuery.fn.serializeObject) ) { // use jquery to ask if the plugins function is already on the page
loadScript("https://cdnjs.cloudflare.com/ajax/libs/jquery-serialize-object/2.5.0/jquery.serialize-object.min.js", function () {
console.log('serialize loaded');
SURGE_start(); // both scrips where not on the website but have now been added so lets run my code now.
});
}
});
} else {
if ( !jQuery.isFunction(jQuery.fn.serializeObject) ) { // use jquery to ask if the plugins function is already on the page
loadScript("https://cdnjs.cloudflare.com/ajax/libs/jquery-serialize-object/2.5.0/jquery.serialize-object.min.js", function () {
console.log('serialize loaded');
SURGE_start(); // jquery was on the web page but the plugin was not included. now we have both scripts lets run my code.
});
} else {
SURGE_start(); // web page already had both scripts so just run my code.
}
}