当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 编写兼容IE和FireFox的脚本

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 编写兼容IE和FireFox的脚本


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 340 ::
收藏到网摘: n/a

编写兼容IE和FireFox的脚本确定的件很烦人的事,今日又经历了一次。 一、正式表达式问题
试图用以下表达式提取中括号“]”后面的内容,连接调用以下代码在IE下正常,FireFox则总是会用单数行的内容替代双数行的内容。
(/(\])([^ -}]*)/ig).exec(srcText);
newtext = "[部门]" + RegExp.$2;
后证实问题出在“ig”的“g”上,FireFox把RegExp当全局变量来处理,且没有及时更新$2,这种问题实在莫名其妙。
二、移除Select的条目
IE支持select.options.remove(index),FireFox却是select.remove(index),兼容的方法是select.options[i] = null;
三、showModalDialog
FireFox可以showModalDialog的了,不过如果showModalDialog出来的页面套了个frame,frame的页面取window.dialogArguments可以成功,FireFox不行,要用window.parent.dialogArguments来取。
四、childNodes
FF没有children,要兼容用childNodes,但IE和FF解释不一致,FF将空白文本、换行当成节点,为避免childNodes(index)取得的节点正确,HTML标识间不要有空格或换行 。
五、removeChild
FF中不能和node.removeNode(true),改为node.parentNode.removeChild(node)。
六、outerHTML
FF没有node.outerHTML,在调用node.parentNode.innerHTML来特别处理,若只是添加事件响应或设属性值,可直接element.onclick = function(){callxxx(x,y);}
七、script域问题
此问题原因不详,FF用<script type="text/javascript" src="inc/lib.js"></script>调用外部JS,在内部script中调用lib.js中的函数时,报告函数未定义,IE则无此问题。