Incompatible JavaScript and unified methods of IE and Firefox

Source: Internet
Author: User

The incompatibility and unified methods of IE and Firefox in JavaScript are summarized as follows:
1. compatible with Firefox's outerhtml and FF, there is no 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 ();
For (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 + "</" + this. tagname. tolowercase () + "> ";
});

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. Collection class Object Problems

Note: in IE, you can use () or [] to obtain collection class objects. In Firefox, you can only use [] to obtain collection class objects.
Solution: use [] to retrieve collection class objects.

3. Custom Attributes

Note: 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 () obtain custom attributes.
Solution: getattribute () is used to obtain custom attributes.

4. eval ("idname") Problems

Note: in IE, you can use eval ("idname") or getelementbyid ("idname") to obtain the HTML object whose ID is idname. In Firefox, you can only use getelementbyid ("idname ") to obtain the HTML object whose ID is idname.
Solution: getelementbyid ("idname") is used to retrieve the HTML object whose ID is idname.

5. The variable name is the same as the ID of an HTML object.

Note: In ie, the ID of the HTML object can be used directly as the variable name of the subordinate object of the document; in Firefox, it cannot. 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 Problems

Note: In Firefox, you can use the const keyword or 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 Problems

Description: The input. Type attribute in IE is read-only, but the input. Type attribute in Firefox is read/write.

8. Window. Event Problems

Note: window. event can only be run in IE, but not in Firefox, because 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

Note: 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 event. X in IE or event. pagex in Firefox.

10. event. srcelement Problems

Note: 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.tar get under firefox.

11. Window. Location. href

Note: in IE or firefox2.0.x, you can use window. Location or window. Location. href; In firefox1.5.x, you can only use window. location.
Solution: Use window. location to replace window. Location. href.

12. Modal and non-modal window Problems

Note: in IE, you can use showmodaldialog and showmodelessdialog to open modal and non-modal windows; in Firefox, no.
Solution: Use window. Open (pageurl, name, parameters) to open a new window.

For example
If you need to pass the parameters in the Child window back to the parent window, you can use window. opener in the Child Window to access the parent window. For example: var parwin =
Window. opener; parwin.doc ument. getelementbyid ("aqing"). value = "aqing ";

13. Frame Problems

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: only window. framename can be used to access this frame object.
In addition, both ieand firefoxcan access this frame object by using the upload metadata Doc ument. getelementbyid ("frameid.

(2) Switch frame content:
In both ieand firefox, you can use Upload upload Doc ument. getelementbyid ("testframe"). src = "xxx.html" or window. framename. Location = "xxx.html" to switch 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. Body Problems

The body of Firefox exists before the body tag is fully read by the browser. The body of IE 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>

15. Event delegation Method

IE: Document. Body. onload = inject; // function inject () has been implemented before this

Firefox: Document. Body. onload = inject ();

Some people say the standard is:

Document. Body. onload = new function ('inject ()');

16. Differences between Firefox and IE (parentelement) parent Elements

IE: obj. parentelement
Firefox: obj. parentnode

Solution: because both Firefox and IE support DOM, using obj. parentnode is a good choice.

17. cursor: hand vs cursor: pointer

Firefox does not support hand, But ie supports pointer

Solution: Use Pointer

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 ";

}

19. The statement similar to OBJ. style. Height = imgobj. Height in Firefox is invalid.

Solution:

OBJ. style. Height = imgobj. height + 'px ';

20. IE, Firefox, and other browsers have different operations on table labels. in IE, assigning values to innerhtml of table and TR is not allowed. When adding a Tr using JS, the appendchile method does not work.

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 );

21. Padding Problems

Padding 5px 4px 3px 1px Firefox cannot be abbreviated,

Must be changed to padding-top: 5px; padding-Right: 4px; padding-bottom: 3px; padding-left: 1px;

22. When the indentation of UL and ol lists is eliminated

Style should be written as: List-style: none; margin: 0px; padding: 0px;

The margin attribute is valid for IE and the padding attribute is valid for Firefox.

23. Transparent CSS

IE: filter: progid: DXImageTransform. Microsoft. Alpha (style = 0, opacity = 60 ).

FF: opacity: 0.6.

24. CSS rounded corners

IE: rounded corners are 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 ;.

25. CSS double-line concave and 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;

Related Article

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.