<script>
function loadScript(url,callback){
var script = document.createElement('script');
script.type = "text/javascript";
if(script.readyState){//ie
script.onreadystatechange = function(){
if(script.readyState == "complete"||script.readyState == "loaded"){
tools[callback]()
}
}
}else{//Safari,Chrome,Firefox,Opera
script.onload = function(){
tools[callback]();
}
}
script.src = url;
document.head.appendChild(script);
}
loadScript('demo.js',"test")
</script>
demo.js
var tools = {
test : function (){
console.log('a');
}
}
<script>
function loadScript(url,callback){
var script = document.createElement('script');
script.type = "text/javascript";
if(script.readyState){//ie
script.onreadystatechange = function(){
if(script.readyState == "complete"||script.readyState == "loaded"){
callback()
}
}
}else{//Safari,Chrome,Firefox,Opera
script.onload = function(){
callback();
}
}
script.src = url;
document.head.appendChild(script);
}
loadScript('demo.js',function(){
test();
})
</script>
demo.js
function test(){
console.log('a');
}