第一个方法,可以使用defer属性.
<
script
language
="javascript"
type
="text/javascript"
src
="http://a.com/bb.js"
defer
="true"
></
script
>
这段script要放在<head></head>之间.而且在脚本中不能有document.write()方法.因为设置了defer="true"的脚本是页面加载之后才加载并渲染的.如果这时候使用document.write()方法,会把之前的页面内容都清掉.
第二个方法,在window.onload之后把script添加到Dom中.
window.onload
=
function
(){
var jsfile = document.createElement( " script " );
jsfile.src = " http://a.com/bb.js " ;
jsfile.type = ' text/javascript ' ;
document.getElementsByTagName( " head " )[ 0 ].appendChild(jsfile);
}
var jsfile = document.createElement( " script " );
jsfile.src = " http://a.com/bb.js " ;
jsfile.type = ' text/javascript ' ;
document.getElementsByTagName( " head " )[ 0 ].appendChild(jsfile);
}