Firefox and IE compatibility issues and solutions

Source: Internet
Author: User

During the development of multi-language java websites, code that can run normally in FF is found to be unavailable in IE, and vice versa. The incompatibility and unified methods of IE and Firefox in JavaScript are summarized as follows: 1. They are compatible with firefox's outerHTML and FF without the outerHtml method. If (window. HTMLElement) {HTMLElement. prototype. _ defineSetter _ ("outerHTML", function (sHTML) {var r = this. ownerDocument. createRange (); r. setStartBefore (this); var df = r. createContextualFragment (sHTML); this. parentNode. replaceChild (df, this); return sHTML;}); HTMLElement. prototype. _ defineGetter _ ("outerHTML", function () {var attr; var attrs = this. attributes; var str = "<" + this. tagName. toLowerCase (); f Or (var I = 0; I <attrs. length; I ++) {= "" attr = "attrs [I];" if (attr. specified) = "" str + = "" + attr. name + '= "' + attr. value + '"'; =" "} =" "if (! This. canHaveChildren) = "" return = "" str + "=" ">"; return str + ">" + this. innerHTML + "" ;}); HTMLElement. prototype. _ defineGetter _ ("canHaveChildren", function () {switch (this. tagName. toLowerCase () {case "area": case "base": case "basefont": case "col": case "frame": case "hr": case "img ": case "br": case "input": case "isindex": case "link": case "meta": case "param": return false ;} return true ;}) ;}2. set Description of Class Object: in IE, you can use () or [] to obtain the set class object. In Firefox, you can only use [] to obtain the set class object. solution: use [] to retrieve collection class objects. 3. description of custom attributes: in IE, you can use the method to obtain general attributes to obtain custom attributes, or use getAttribute () to obtain Custom Attributes. In Firefox, you can only use getAttribute () to obtain custom attributes. solution: getAttribute () is used to obtain custom attributes. 4. eval ("idName") Problem description: in IE, you can use eval ("idName") or getElementById ("idName") to obtain the HTML object whose id is idName; in Firefox, only getElementById ("idName") can be used to obtain the HTML object whose id is idName. solution: Use getElementById ("idName") to obtain the HT whose id is idName. ML object. 5. the variable name is the same as the ID of an HTML object. In IE, the ID of an HTML object can be directly used as the variable name of a subordinate object of the document. In Firefox, the variable name cannot be used. in Firefox, you can use the same variable name as the HTML Object ID; in IE, you cannot. Solution: Use document. getElementById ("idName") replaces document. idName. we recommend that you do not use variable names with the same HTML Object ID to reduce errors. When declaring variables, add var to avoid ambiguity. 6. const Problem description: In Firefox, you can use the const keyword or the var keyword to define constants. in IE, you can only use the var keyword to define constants. solution: Use the var keyword to define constants. 7. input. type Attribute Problem description: input in IE. the type attribute is read-only, but input in Firefox. the type attribute is read/write. 8. window. event Description: window. the event can only run in IE, but not in Firefox, because the Firefox event can only be used in the event. solution: IE: <input name = "Button8_1" type = "button" value = "IE" onclick = "javascript: gotoSubmit8_1 ()"/>... <script language = "javascript"> function gotoSubmit8_1 (){... alert (window. event); // use window. event ...} </script> IE & Firefox: <input name = "Button8_2" type = "button" value = "IE" onclick = "javascript: gotoSubmit8_2 (event)"/>... <script language = "javascript"> function gotoSubmit8_2 (evt ){... evt = evt? Evt :( window. event? Window. event: null); alert (evt); // use evt ...} </script> 9. event. x and event. y Problem description: in IE, the even object has the x and y attributes, but does not have the pageX and pageY attributes. In Firefox, the even object has the pageX and pageY attributes, but does not have the x and y attributes. solution: Use mX (mX = event. x? Event. x: event. pageX;) to replace the event in IE. event in x or Firefox. pageX. 10. event. srcElement Problem description: in IE, the even object has the srcElement attribute, but does not have the target attribute. In Firefox, the even object has the target attribute, but does not have the srcElement attribute. solution: Use obj (obj = event. srcElement? Event. srcElement: event.targettoken used to replace event.srcelement?event.tar get under firefox. 11. window. location. href Problem description: windows can be used in IE or Firefox2.0.x. location or window. location. href; In Firefox1.5.x, only windows can be used. location. solution: Use window. location to replace window. location. href. 12. modal and non-modal windows Description: in IE, you can use showModalDialog and showModelessDialog to open modal and non-modal windows; in Firefox, you cannot. solution: directly use window. open (pageURL, name, parameters) to open a new window. If you want to pass the parameters in the Child window back to the parent window, you can use window in the Child window. opener to access the parent window. for example, var parWin = window. opener; parWin.doc ument. getElementById ("Aqing "). value = "Aqing"; 13. the following frame is used as an example: <frame src = "xxx.html" id = "frameId" name = "frameName"/> (1) access the frame object: IE: Use window. frameId or window. frameName to access this frame object. firefox: You can only use zookeeper framenameto access this framework.additionally, both ieand firefoxcan use zookeeper Doc ument. getElementById ("frameId") to access this frame object. (2) Switch f Rame content: Both ieand firefoxcan use zookeeper Doc ument. getElementById ("testFrame "). src = "xxx.html" or window. frameName. location = "xxx.html" to switch the frame content. if you want to return parameters in the frame to the parent window, you can use parent in frme to access the parent window. Example: parent.doc ument. form1.filename. value = "Aqing"; 14. the following getElementByClass is used as an example: document. getElementByClass ("classname1"); this function does not work in IE and can be replaced by getElementsByClassName. However, this function returns a match with NodeList instead of a single object, such as var list, index; list = document. getElementsByClassName ("classname1"); for (index = 0; index <list. length; ++ index) {list [index]. setAttribute (/*... */);} for such issues, it is best to use class libraries such as jQuery, Prototype, and Google Closure., Etc., these class libraries are compatible with all browsers. This saves a lot of time to handle these compatibility issues. For instance, in jQuery: $ (". home1 "). attr (/*... */); 15. body Problem: Firefox's body exists before the body tag is fully read by the browser, While IE's body must exist only after the body tag is fully read by the browser. for example, Firefox: <body> <script type = "text/javascript"> document. body. onclick = function (evt) {evt = evt & #124; & #124; window. event; alert (evt) ;}</script> </body> IE & Firefox: <body> </body> <script type = "text/javascript"> document. body. onclick = function (evt) {evt = evt & #124; & #124; Window. event; alert (evt) ;}</script> event Delegate method IE: document. body. onload = inject; // Function inject () has been implemented by Firefox: document. body. onload = inject (); some people say the standard is: document. body. onload = new Function ('inject () '); the difference between the parent element of firefox and IE (parentElement) IE: obj. parentElement firefox: obj. parentNode solution: because firefox and IE both support DOM, obj is used. parentNode is a good choice. 17. cursor: hand VS cursor: pointer firefox does not support hand, But ie supports pointer solution: Use poi Nter 18. innerText works normally in IE, but innerText does not work in FireFox. solution: if (navigator. appName. indexOf ("Explorer")>-1) {document. getElementById ('element '). innerText = "my text";} else {document. getElementById ('element '). textContent = "my text";} similar to obj. style. height = imgObj. the height statement is invalid. Solution: obj. style. height = imgObj. height + 'px '; operations on table labels vary with IE, firefox, and other browsers. in ie, values of innerHTML of table and tr cannot be assigned. js increments are used. When a tr is added, the appendChile method is not used. Solution: // append an empty row to the table: var row = otable. insertRow (-1); var cell = document. createElement ("td"); cell. innerHTML = ""; cell. className = "XXXX"; row. appendChild (cell); padding problem: padding 5px 4px 3px 1px FireFox cannot be abbreviated. It must be changed to padding-top: 5px; padding-right: 4px; padding-bottom: 3px; padding-left: 1px; the style should be written as list-style: none; margin: 0px; padding: 0px when the ul and ol lists are removed. The margin attribute is valid for IE, the padding attribute is valid for FireFox. CSS transparency: IE: filter: pro Gid: DXImageTransform. Microsoft. Alpha (style = 0, opacity = 60 ). FF: opacity: 0.6. CSS rounded corner IE: the rounded corner is not supported. FF:-moz-border-radius: 4px, or-moz-border-radius-topleft: 4px;-moz-border-radius-topright: 4px; -moz-border-radius-bottomleft: 4px;-moz-border-radius-bottomright: 4px ;. CSS double-line concave-convex border IE: border: 2px outset; FF:-moz-border-top-colors: # d4d0c8 white;-moz-border-left-colors: # d4d0c8 white; -moz-border-right-colors: #404040 #808080;-moz-border-bottom-colors: #404040 #808080 conclusion: it is best to use class libraries such as jQuery, prototype, Google Closure, etc ., these class libraries are compatible with all browsers. this will save a lot of time to deal with these compatibility issues.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.