Uncaught TypeError: Object # has no method 'load'

一劍平江湖發表於2014-01-12
當Javascript載入XML檔案時,IE和FF下正常,但Chrome下報錯:Uncaught TypeError: Object #<Document> has no method 'load' ,這是因為Chrome瀏覽器沒有實現XMLdocument.load方法,你可以換成XmlHttpRequest 
var xmlDoc = "";
try{ //Internet Explorer
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(file);
}catch(e){
  try{ //Firefox, Mozilla, Opera, etc.
    xmlDoc=document.implementation.createDocument("","",null);
    xmlDoc.async=false;
    xmlDoc.load(file);
  }catch(e){
    try{ //Google Chrome
      var xmlhttp = new window.XMLHttpRequest();
      xmlhttp.open("GET",file,false);
      xmlhttp.send(null);
      xmlDoc = xmlhttp.responseXML.documentElement;
    }catch(e){
      error=e.message;
    }
  }
}

相關文章