The difference between IE and Firefox in JS

Source: Internet
Author: User
Tags eval variable window
JS code in the difference between IE and Firefox, 16, very practical.

1. The ID of an HTML object as an object name problem
The ID of the Ie:html object can be used directly as the subordinate object variable name of the document
FF: Not supported
WORKAROUND: Use document.getElementById instead of document.all

2. If the control has only name and no ID, when using getElementById:
IE: You can find the object
FF: Return null
Workaround: All controls must set the id attribute

3. Evel:
IE: Support, using eval (idname) to get HTML objects with ID idname
FF: Not supported.
Workaround: Unify the use of getElementById (idname) instead of eval (idname).

4. Event:
IE: Window.event with Global object
Ff:event can only be used on the spot where the event occurs, passing in the event object when the function is called
Workaround: Use the browser type judgment before using each.

5. Change the dollar sign ' $ ' in the object name to use the underscore ' _ '

6. Collection Class object problem
IE: You can use () or [] to get collection class objects
FF: Only use [] to get collection class objects
There are many existing code that cannot be run under FF
WORKAROUND: Use [] to get collection class objects uniformly. Document.form.item ("ItemName") change the statement to document.form.elements["ElementName"]

7. Variable name and an HTML object ID the same problem
IE: Cannot use variable names that are the same as HTML object IDs
FF: You can use
Solution: When declaring variables, all add Var, in order to avoid ambiguity, so that in IE can also be normal operation.
In addition, it is best not to take variable names that are the same as the HTML object IDs to reduce errors.

8. Event positioning problem
IE: Support for Event.x and EVENT.Y
FF: Support for Event.pagex and Event.pagey
Workaround, unify the use of Event.clientx and event.clienty, but there is a subtle difference between event.clientx and Event.pagex in FF (when the entire page has scroll bars), but most of the time it is equivalent. If you want to be exactly the same, you can determine the browser type after the corresponding use.

9. Problems with parent nodes
Ie:parentelement Parentelement.children
Ff:parentnode Parentnode.childnodes
ChildNodes the meaning of the subscript is different in IE and FF, FF uses the DOM specification, and a blank text node is inserted in ChildNodes. It is generally possible to avoid this problem by Node.getelementsbytagname ().


When nodes in HTML are missing, IE and FF have different interpretations of parentnode, such as
<form>
<table>
<input/>
</table>
</form>
The IE:input.parentNode value is an empty node
The value of the FF:input.parentNode is form
There is no Removenode method for nodes in FF, you must use the following method Node.parentNode.removeChild (node)

The question of const
Existing issues:
IE: The const keyword is not supported. such as const CONSTVAR = 32; This is a syntax error in IE.
FF: Support
Workaround: Do not use const instead of Var.

One. Body Object
IE: only exists after the body tag is fully read
FF: Exists before the body tag is fully read by the browser

12. Custom Attribute Issues
IE: You can obtain custom attributes by using the method that gets the general properties, or you can use GetAttribute () to get the defined attributes
FF: Only getattribute () can be used to derive the defined attribute.
Workaround: Unified through GetAttribute () derived from defined attributes

Question of Event.srcelement
IE:even.srcElement
FF:even.target
Workaround: Use to determine the browser type, each with its own.

14. Modal and non-modal windows
IE: Supports modal and non modal Windows
FF: Not supported
WORKAROUND: Open the new window directly using the window.open (Pageurl,name,parameters) method. If you need to pass parameters from a child window back to the parent window, you can use Window.opener in the child window to access the parent window.

InnerText
Ie:innertext
Ff:textcontent

16. Statements similar to Obj.style.height = Imgobj.height
IE: Effective
FF: Invalid
Solution: Unified Use of Obj.style.height = Imgobj.height + ' px ';



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.