JavaScript可以Import导入么?
在做网页的时候,很多页面都要用到共同的JAVASCRIPT,而这些代码都很长。想请教一下,JAVASCRIPT对网页,能像DTD对XML那样的外部导入么?
[解决办法]
function yourimport(inImpPath){
var i;
var basePath;
var srcFile = "your.js ";
var srcPath = "js/your.js ";
var scripts = document.getElementsByTagName( "script ");
for (i=0; i <scripts.length; i++){
if (scripts[i].src.match(srcPath)) {
basePath= scripts[i].src.replace(srcFile, " ");
}
if(scripts[i].src.match(inImpPath)) {
return;
}
}
document.write( " < " + "script src=\ " " + basePath+ inImpPath + "\ "> </ " + "script> ");
}
function doDefaultInit() {
yourimport( "xxx1.js ");
yourimport( "xxx2.js ");
}
[解决办法]
用这段代码:
<script Language= "JavaScript " src= "/script/test.js "> </script>
放在html页面里面,那么test.js里面的函数就可以随意调用了。
试试看吧。