我在加载页面后根据以下条件加载脚本:
function isScriptAlreadyIncluded(src){
var scripts = document.getElementsByTagName("script");
for(var i = 0; i < scripts.length; i++){
if(scripts[i].getAttribute('src') === src) return true;
else return false;
}
}
if(isScriptAlreadyIncluded('contextualConversation.js')) contextualReplace();
else{
var cCScript = document.createElement('script');
cCScript.src = 'contextualConversation.js';
contextualReplace();
}
我的问题是contextualReplace()
,我相信在脚本加载之前,将在else中执行。那么,如何在刚添加的脚本加载完成后告诉它运行该功能?
除了版本9之前的IE之外,脚本都会触发事件onload-
else{
var cCScript = document.createElement('script');
cCScript.onload=contextualReplace;
cCScript.src = 'contextualConversation.js';
}
(要覆盖较旧的IE,您可以使用它的readystatechange事件,请检查Microsoft开发人员网络)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句