`

IE和Firefox浏览器下javascript、CSS兼容性研究

阅读更多

为了将公司的产品在IE和Firefox下达到兼容,前段时间做了如下研究。由于之前准备的就是英文文档,我也懒得再翻译成中文了,呵呵。

首先你要在每个页面执行javascript之前引入下面这个我做好的兼容文件。


IEFirefox.js


1.      obj.firstChild/.lastChild/.nextSibling/.childNodes/.previousSibling should be changed.

2.      Assign a property “id” to HTML element if it miss “id”

3.      Keep parameters case-sensitive between file.js and file.cs

4.      Using getElementById(objId) to get a object instead of eval(objId)

5.      Add <tr> between <thead>and<th>

6.      Change aRows(i).cells to aRows[i].cells

7.      Using standard way to get/set customized value

8.      Using standard way to remove an option.

9.      Firefox doesn’t support Expression in style file.

10.         Change the event onmouseleave() to onmouseout()

11.         Change obj.fireEvent(eventname) to fireEvent(obj,eventname)

12.         Don’t use the command document.readyState!="complete"

13.         Don’t use window.createPopup()

14.         Change document.body.scrollLeft to document.documentElement.scrollLeft

15.         Firefox dosen’t support filter property

16.         Add a postfix ‘px’ to specify the width/height or position

17.         Change style=”cursor:hander” to style=”cursor:pointer”

18.         Don’t forget propertys “title” and “alt” for img element

19.         FireFox do not support the style “display:block” into <tr>

20.         Don’t forget setting opacity for firefox

21.         Have browsers IE and FireFox compatible in .css

 

 
  1. obj.firstChild/.lastChild/.nextSibling/.childNodes/.previousSibling should be changed.

Some functions exist in IE and Firefox, but they might implement different functionality, you can change them into our predefined function in SalIEFirefox.js.

Not compatible:

var wrongGet = obj.firstChild ;

var wrongGet = obj.lastChild ;

var wrongGet = obj.nextSibling ;

var wrongGet = obj.childNodes ;

var wrongGet = obj.previousSibling ;

Compatible

var rightGet = getFirstChild (obj)

var rightGet = getLastChild (obj)

var rightGet = getNextSibling (obj)

var rightGet = getChildNodes (obj)

var rightGet = getPreviousSibling (obj)

 

  1. Assign a property “id” to HTML element if it miss “id”

Add “id” for every HTML element, because if there is only “name” for HTML element, IE will assign the “name” value to “id”, but Firefox will not.

Not compatible:

tmpHtml.Append("<input type=/"text/" name=/"" + str1 + "/" value=/"0/">" );

Compatible:

tmpHtml.Append("<input type=/"text/" name=/"" + str1 + "/" id=/"" + str1 + "/" value=/"0/">" );

 

  1. Keep parameters case-sensitive between file.js and file.cs

It is case-sensitive for HTML element’s id and any parameter in Firefox

Not compatible:

.js var tableDrag= document.getElementById(SectionId+"_dataTable" );

.cs       sbdTempHtml.Append("<table id=/"" + SectionId + "_datatable /">" );

Compatible:

.js var tableDrag= document.getElementById(SectionId+"_dataTable" );

.cs       sbdTempHtml.Append("<table id=/"" + SectionId + "_dataTable /">" );

 

  1. Using getElementById(objId) to get a object instead of eval(objId)

Don’t use “eval” to cast a string to Object, in other words, using GetElementById(strObjId) instead of eval(strObjId)

Not compatible:

objField1 = eval ("document.mainform.meritid" + i);

Compatible:

objField1 = document.getElementById ("document.mainform.meritid" + i);

 

You should be careful of the following:

Compatible:

var objAjax = eval ("SalaryCom.CompPlanner.CppElementScripts." + document.mainform.aaa.value);

 

  1. Add <tr> between <thead>and<th>

Add <tr> between <thead>and<th>, because in IE it will auto add <tr> for it, but Firefox will not. Then when you are trying to get some element using obj.parentNode() might be different.

Not compatible:

sbdTempHtml.Append("<table>" );

sbdTempHtml.Append("<thead>" );

sbdTempHtml.Append("<th width=/"100/">test field name 1</th>" );

sbdTempHtml.Append("<th width=/"200/">test field name 2</th>" );

sbdTempHtml.Append("</thead>" );

sbdTempHtml.Append("<table>" );

Compatible:

sbdTempHtml.Append("<table>" );

sbdTempHtml.Append("<thead>" );

sbdTempHtml.Append("<tr>" );

sbdTempHtml.Append("<th width=/"100/">test field name 1</th>" );

sbdTempHtml.Append("<th width=/"200/">test field name 2</th>" );

sbdTempHtml.Append("</tr>" );

sbdTempHtml.Append("</thead>" );

sbdTempHtml.Append("<table>" );

 

  1. Change aRows(i).cells to aRows[i].cells

Not compatible:

aRows(i) .cells

Compatible:

aRows[i] .cells

 

  1. Using standard way to get/set customized value

Using the following standard way to get/set customized value for HTML element.

 

Not compatible:

var str = Obj.customizedvalue ;

Compatible:

var str = Obj.getAttribute( customizedvalue ”) ;

 

  1. Using standard way to remove an option.

Using the following standard way to remove an option in selected element.

Not compatible:

oSel.options.remove (oSel.selectedIndex);

Compatible:

oSel.remove (oSel.selectedIndex);

 

  1. Firefox doesn’t support Expression in style file.

Not compatible:

top : expression (parentNode.parentNode.parentNode.parentNode.scrollTop) ;

width :expression (document.getElementById('CenterDIV').offsetWidth-16+'px') ;

Compatible:

        Consider to use JS method instead of using expression in css.

 

  1. Change the event onmouseleave() to onmouseout()

There is no event of onmouseleave() in Firefox, you should change it to onmouseout(),but be careful to change it like following

Not compatible:

div.attachEvent("onmouseleave" ,new Function("clearPopUpMenu();" ));

Compatible:

div.attachEvent("onmouseout" ,new Function("clearPopUpMenu();" ));

 

  1. Change obj.fireEvent(eventname) to fireEvent(obj,eventname)

There is no method obj.fireEvent() in Firefox, you should change it to following:

Not compatible:

div.fireEvent( "onscroll");

Compatible:

fireEvent(div, "onscroll");

 

  1. Don’t use the command document.readyState!="complete"

Firefox doesn’t support this command document.readyState!="complete"

Not compatible:

              if (document.readyState!="complete" )

 

  1. Don’t use window.createPopup()

Don’t use window.createPopup() method to create a popup window.

Not compatible:

           window.createPopup();

 

  1. Change document.body.scrollLeft to document.documentElement.scrollLeft

There are some differences between body.scrollLeft and other HTML element(documentElement.scrollLeft), you should care about it.

Not compatible:

var _left = document.body.scrollLeft;

Compatible:

var _left = document.documentElement.scrollLeft;

      

you should be careful of the following propertys which should be also applied in:

scrollHeight|scrollLeft|scrollTop|scrollWidth

 

  1. Firefox dosen’t support filter property

A file Cppu_ColorGradient.js can resolve the problem, include the file in Cppb_Header.ascx.cs and do something such as set classname and get client color and so on…

 

  1. Add a postfix ‘px’ to specify the width/height or position

Not compatible:

document.GetElementById(strObjId).style.width = 10;

Compatible:

document.GetElementById(strObjId).style.width = ‘10px’;

 

you should be careful of the following propertys which should be also applied in (you can ignore if it is a read only property).

width|height|right|left|scrollHeight|scrollWidth|scrollLeft|scrollTop|offsetHeight|offsetWidth|offsetLeft|offsetTop|clientHeight|clientWidth|clientLeft|clientTop|lineHeight|lineWidth

 

  1. Change style=”cursor:hander” to style=”cursor:pointer”

Not compatible:

style=”cursor:hander [k1]  

Compatible:

style=”cursor:pointer

 

  1. Don’t forget propertys “title” and “alt” for img element

You should assign “title” and “alt” property for img element. Because it will atuo assign “alt” value to “title” property in IE, while it will not in Firefox.

Not compatible:

sbdTempHtml.Append("<img src=/"../Graphics/i_expand.gif/" /></div>" );

Compatible:

sbdTempHtml.Append("<img alt=/"/" title=/"/" src=/"../Graphics/i_expand.gif/" /></div>" );

 

  1. FireFox do not support the style “display:block” into <tr>

we are using display:block on tr tag which is not correct in Firefox. After applying display:block, the layout of the table is broken. The default style for tr in Firefox should be ‘display:table-row’

Not compatible:

document.getElementById("hrmtr" ).style.display = "block" ;

Compatible:

if (window.isIE)

document.getElementById("hrmtr" ).style.display = "block" ;

else

document.getElementById("hrmtr" ).style.display = "" ;

 

 

  1. Don’t forget setting opacity for firefox

It is only applied in IE if you set opacity as “filter:alpha(opacity=50);”,

Not compatible:

filter :alpha(opacity=50) ;

Compatible:

filter :alpha(opacity=50) ;

-moz-opacity :0.5 ; /*css*/

 

/*The way in js*/

if (!window.isIE)

obj.style.MozOpacity = 0.5;

 

  1. Have browsers IE and FireFox compatible in .css

If you want to have browsers IE & FireFox compatible in .css, you should copy a line and prefixed “*”, and the line must be under the original line, then Firefox is hight priority automatically, IE will ignore it and only process a line prefixed “*”.

Not compatible:

margin :10px ;

Compatible:

  margin :20px ; /*for firefox*/

  *margin :10px ;     /*for ie7,ie6 */

分享到:
评论

相关推荐

    IE和Firefox在css,JavaScript方面的兼容性

    IE和Firefox在css,JavaScript方面的兼容性

    javascript和css在IE和Firefox中的不同点及解决兼容性的方案

    javascript和css在IE和Firefox中的不同点及解决兼容性的方案,一共有二十五点

    javascript和css在IE和Firefox中的不同点

    javascript和css在IE和Firefox中的不同点

    IE和Firefox浏览器CSS网页布局不同点

    我们讨论的主题CSS网页布局,最令大家头疼的问题就是浏览器兼容性,虽然jb51.net介绍过很多这方向的知识,但依然让很多开发人员晕头转向,今天的这篇文章,将列出css和javascript在IE和Firefox中二十三个不同点,...

    css与javascript跨浏览器兼容性总结

    主要介绍了css与javascript跨浏览器兼容性,包括常见的css兼容性问题与javascript兼容性问题,以及IE与Firefox等常用浏览器的兼容性分析,需要的朋友可以参考下

    IE与FF的兼容问题

    IE与FF的兼容问题 IE浏览器和火狐浏览器兼容问题——CSS篇 IE浏览器和Firefox(火狐)浏览器兼容性——Javascript篇

    javascript css在IE和Firefox中区别分析

    我们讨论的主题CSS网页布局,最令大家头疼的问题就是浏览器兼容性,虽然52CSS.com介绍过很多这方向的知识,但依然让很多开发人员晕头转向,今天的这篇文章,将列出css和javascript在IE和Firefox中二十三个不同点,...

    javascript+css贪吃蛇

    javascript+CSS版本贪吃蛇,兼容性没做处理,火狐、IE11下可以,其他版本没有测试

    2013年五大主流浏览器 HTML5 与 CSS3 兼容性大比拼

    这篇文章给大家带来《五大主流浏览器 HTML5 和 CSS3 兼容性大比拼》,让我们一起来看看2013年的浏览器现状。      浏览器厂商之间的竞争促使各大浏览器对 HTML5 和 CSS3 的支持越来越完善,下面的图表列...

    Dom与浏览器兼容性说明

    作为一个Web前端工作者,你...我将在下面列举部份Dom指令在IE和FireFox等浏览器 不兼容性的问题!限于篇幅.我不在该页对提到的Dom指令做详细解释.请点击相关指令名称可以了解详细解释与实例.在使用JavaScript获取触

    IE和Firefox在JavaScript应用中的兼容性探讨

    今天在使用CSS属性的时候发现"cursor:hand;"在Firefox中鼠标不会变为手型,后来上网搜索了一下资料,发现hand这个cursor属性在Firrefox中不兼容,使用"cursor:pointer"就都可以显示了。

    Javascript 多浏览器兼容性问题及解决方案

    CSS 多浏览器兼容性问题及解决方案一、document.formName.item(”itemName”) 问题 问题说明:IE下,可以使用 document.formName.item(”itemName”) 或 document.formName.elements [“elementName”];Firefox 下...

    页面在360浏览器下显示不正常的兼容性问题

    但是在Firefox、chrome和IE8+以上的浏览器上,都是显示正常的。 问题的分析 1. 检查了一些Javascript框架,标准的jquery类库1.x系列,确认其工作正常,问题不是在于Javascript方面。 2. 排查掉HTML标签内容的...

    浏览器兼容性问题大汇总

    JavaScript 1.HTML对象获取问题 FireFox:document.getElementById(“idName”); ie:document.idname或者document.getElementById(“idName”). 解决办法:统一使用document.getElementById(“idName”); 2....

    JavaScript 实例 精粹 整理

    包括:11种用javascript做的刷新按钮的方法、40种Javascript中常用的使用小技巧、Javascript的IE和Firefox兼容性汇编、JavaScript函数大全、JavaScript实际应用:innerHTMl和确认提示的使用、 CSS的常用技巧、IP地址...

    javascript脚本化文档

    这一Netscape 4 DOM是革新的尽头,它只被Netscape 4支持,并且在由Netscape代码基础上扩展而来的Mozilla和Firefox浏览器中也被抛弃了。对Netscape 4 DOM的介绍也从本书的这一版本中删除掉了。 本章的大部分内容介绍...

    js-css-clock:该项目是使用CSS,CSS动画和JavaScript创建的时钟

    我最终进一步研究了跨浏览器的兼容性问题,而FireFox的问题与Safari相同。 我还必须考虑到它,然后必须重新调整Safari转换属性。 为了安全起见,我最终将-moz和-webkit添加到所有转换和转换中。 mozTransform也已...

Global site tag (gtag.js) - Google Analytics