在IE或壹些基於IE內核的瀏覽器中(如Maxthon),它是通過script節點的readystatechange方法來判斷的,而其它的壹些瀏覽器中,往往是通過load事件來決定的,如下代碼:
function dynamicLoad(){var _doc=document.getElementsByTagName('head')[0];
var script=document.createElement('script');
script.setAttribute('type','text/javascript');
script.setAttribute('src','jquery-1.4.4.js');
script.onload=script.onreadystatechange=function(){
if(!this.readyState||this.readyState=='loaded'||this.readyState=='complete'){
alert('done');}script.onload=script.onreadystatechange=null;}}
這是因為,如果script節點沒有添加到DOM樹中,那麽在chrome和firefox中是不會響應script的load事件的。但是IE中卻可以。。修改後如下代碼:
function dynamicLoad(){var _doc=document.getElementsByTagName('head')[0];
var script=document.createElement('script');
script.setAttribute('type','text/javascript');
script.setAttribute('src','jquery-1.4.4.js');
_doc.appendChild(script);
script.onload=script.onreadystatechange=function(){
if(!this.readyState||this.readyState=='loaded'||this.readyState=='complete'){
這時候,所有的瀏覽器都可以響應,妳可以嘗試壹下~