javascript在中ie與firefox的區別與解決方案
1.載入xml
ie建立對象:
msXmlAx=new ActiveXObject("Microsoft.XMLDOM");
firefox建立對象:
xDoc=document.implementation.createDocument("","",null);
總體方法為:
///判斷瀏覽器 建立不同的對象
function getXmlDocument()
{
var xDoc;
if(document.implementation && document.implementation.createDocument)
{
xDoc=document.implementation.createDocument("","",null);
}
else if(typeof ActiveXObject !="undefined")
{
var msXmlAx=null;
try{
msXmlAx=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
msXmlAx=new ActiveXObject ("Microsoft.XMLDOM");
}
xDoc=msXmlAx;
}
if(xDoc== null || typeof xDoc.load=="undefined")
{
xDoc=null;
}
return xDoc;
}
2.insertRow和insertCell
在ie中可直接調用該方法 document.getElementById("xxx").insertRow();
在firefox 中需要放置參數:document.getElementById("xxx").insertRow(-1);
同樣 insertRow(-1)也支援ie
3.判斷瀏覽器類型
/*---------------------------------------------------------------
--this function can return the actual browser name and version.--
--USESAGE:There are Two Methods(See the end of this function) --
--Create By Yemoo. DateTime:2006-2-3 21:53:37 --
---------------------------------------------------------------*/
function browserinfo(){
var Browser_Name=navigator.appName;
var Browser_Version=parseFloat(navigator.appVersion);
var Browser_Agent=navigator.userAgent;
var Actual_Version,Actual_Name;
var is_IE=(Browser_Name=="Microsoft Internet Explorer");
var is_NN=(Browser_Name=="Netscape");
if(is_NN){
//upper 5.0 need to be process,lower 5.0 return directly
if(Browser_Version>=5.0){
var Split_Sign=Browser_Agent.lastIndexOf("/");
var Version=Browser_Agent.indexOf(" ",Split_Sign);
var Bname=Browser_Agent.lastIndexOf(" ",Split_Sign);
Actual_Version=Browser_Agent.substring(Split_Sign+1,Version);
Actual_Name=Browser_Agent.substring(Bname+1,Split_Sign);
}
else{
Actual_Version=Browser_Version;
Actual_Name=Browser_Name;
}
}
else if(is_IE){
var Version_Start=Browser_Agent.indexOf("MSIE");
var Version_End=Browser_Agent.indexOf(";",Version_Start);
Actual_Version=Browser_Agent.substring(Version_Start+5,Version_End)
Actual_Name=Browser_Name;
if(Browser_Agent.indexOf("Maxthon")!=-1){
Actual_Name+="(Maxthon)";
}
else if(Browser_Agent.indexOf("Opera")!=-1){
Actual_Name="Opera";
var tempstart=Browser_Agent.indexOf("Opera");
var tempend=Browser_Agent.length;
Actual_Version=Browser_Agent.substring(tempstart+6,tempend)
}
}
else{
Actual_Name="Unknown Navigator"
Actual_Version="Unknown Version"
}
/*------------------------------------------------------------------------------
--Your Can Create new properties of navigator(Acutal_Name and Actual_Version) --
--Userage: --
--1,Call This Function. --
--2,use the property Like This:navigator.Actual_Name/navigator.Actual_Version;--
------------------------------------------------------------------------------*/
navigator.Actual_Name=Actual_Name;
navigator.Actual_Version=Actual_Version;
/*---------------------------------------------------------------------------
--Or Made this a Class. --
--Userage: --
--1,Create a instance of this object like this:var browser=new browserinfo;--
--2,user this instance:browser.Version/browser.Name; --
---------------------------------------------------------------------------*/
this.Name=Actual_Name;
this.Version=Actual_Version;
}
通過: navigator.Actual_Name 得到瀏覽器名稱,navigator.Actual_Version 的到瀏覽器版本
4. firefox 中無法像ie一樣通過documentElement得到xml文字物件的所有元素資訊,可以使用替換法:
建立XMLHttpRequest對象,通過它的responseXML返回的xml對象進行操作
5.firefox中不支援window.event
解決方案:
document.onclick=function(e) //要相容ff的話,這個參數一定要寫上
{
e = window.event || e;
e = e.srcElement || e.target;
//其他實現代碼
}
6.firefox不支援parentElement,若要使用可寫成:parentNode
7.IE中將xml通過xsl進行格式化時,使用如下代碼:
xmlDoc.transformNode(xslDoc);
xmlDoc為xml文檔
xslDoc為xsl文檔
在firefox中使用如下代碼:
var xsltProcessor =new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
var resultHtml= xsltProcessor.transformToDocument(xmlDoc);
var oXmlSerializer = new XMLSerializer();
$GetEle("ZfDiv_").innerHTML= oXmlSerializer.serializeToString(resultHtml);
8.ifream載入中 使iframe的高度根據所載入頁的高度而改變
網上有代碼為:
<iframe onload="document.all.demo.height=document.frames['demo'].document.body.scrollHeight" src="demo.htm" />
代碼沒有問題,只是在Firefox中不能實現想要的效果
改為:
document.getElementById('demo').height=window.frames['demo'].document.body.scrollHeight
即可