本文介绍了加载脚本后调用JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< script type =我有一个html页面,我在这里动态地添加html。 text / javascriptsrc =/ myapp / htmlCode>< / script>
我想调用一个js函数,例如loadedContent();一旦上面的脚本添加了动态HTML。
有人可以帮助我如何做到这一点吗?
解决方案
您可以在不使用head.js javascript的情况下实现此目的。
函数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 ===完成){
script.onreadystatechange = null;
callback();
}
};
} else {//其他
script.onload = function(){
callback();
};
}
script.src = url;
document.getElementsByTagName(head)[0] .appendChild(script);
//调用函数...
loadScript(pathtoscript,function(){
alert('script ready!');
});
I have a html page where I am appending html at dynamically through a javascript like below
<script type="text/javascript" src="/myapp/htmlCode"></script>
I want to call a js function e.g. loadedContent(); once the above script adds dynamic html.
Can someone help me how I can do that?
解决方案
you can achieve this without using head.js javascript.
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 );
}
// call the function...
loadScript(pathtoscript, function() {
alert('script ready!');
});
这篇关于加载脚本后调用JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!