【发布时间】:2011-12-18 02:02:08
【问题描述】:
我尝试动态加载一些js文件,例如:
function openInforWindow(){
//check if the InforWinow.js has been loaded or not
if(window.InforWindow){
//do the right thing
}
else {
loadJs('xxxxxx/InforWindow.js');
// do the right thing
//but here ,the infowindow is not definded yet.
}
}
function loadJs(filename){
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
如何确保动态加载的js中的变量或函数可以添加到javascript执行环境中以便我可以使用它们?
【问题讨论】:
-
你在哪里使用参数“文件名”?我猜“js”应该是文件名。在行 fileref.setAttribute("src",filename);
-
你还是弄错了,即它仍然在双引号内,这使它成为字符串而不是变量。
标签: javascript