Some common JavaScript compatibility summaries, javascript compatibility

Source: Internet
Author: User

Some common JavaScript compatibility summaries, javascript compatibility

1. document. formName. item ("itemName ")
Note: in IE, you can use document. formName. item ("itemName") or document. formName. elements ["elementName"];
In Firefox, only document. formName. elements ["elementName"] can be used.
Solution: Use document. formName. elements ["elementName"].

2. Collection objects
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. However, we recommend that you directly access element attributes through the "vertex" operator.

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: Use getElementById ("idName") to retrieve the HTML object with id as 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. It is best not to 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.
Solution: You can create two input boxes. For example, if we usually have a password box and want to change the value of type to password to text, it is not allowed in IE.

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. Firefox must be added to the source for parameter transfer. IE ignores this parameter and uses window. event to read this event.
Solution: var e = e | window. event;

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 var x = e. x? E. x: e. pageX; to replace event. x in IE or e. pageX in Firefox;

10. event. srcElement Problems
Note: In IE, the event 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 event.srcelement1_firefox. pay attention to the compatibility of event.

11. body Problems
Note: 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.

12. Differences between firefox and IE parent Elements
Note: IE supports parentElement and parentNode to obtain the parent node, while FF can only use the latter.
Solution: Use parentNode in a unified manner;

13. innerText Problems
Note: innerText works normally in IE, but innerText does not work in FireFox. textContent is required.
Solution: elem. innerText = elem. textContent = "value"

14. Style Unit issues
Note: When setting the HTML label style in FireFox, the values of all locations and font sizes must be followed by px. This ie is also supported.
15. style keyword conflicts
Note: CSS attributes are the same as reserved keywords in Javascript. style + attributes in IE are not css + attributes in IE.

16. class Attribute conflicts
Note: class Attribute conflicts. class is a reserved keyword in javascript.

17. Year Acquisition Problems
Note: Use getFullYear to replace getYear.

18. for attribute conflicts
Note: The attribute for conflict of the lable label. in IE browser, getAttribute ("htmlFor") and getAttribute ("for") are not in IE browser ").
19. removeChild and removeNode Problems
Note: appendNode can be used normally in IE and Firefox, but removeNode can only be used in IE, and FF supports removeChild.

20. event listening function problems
Note: The standard browser write method addEventListener () and IE write method attachEvent ().
Solution: Determine whether addEventListener exists. If so, use the binding method attachEvent, removeEventListener () and detachEvent () of IE8 or earlier versions.

21. Prevent event bubbles
Description: stopPropagation () and cancelBubble. The former is a method and a standard writing method. The latter is an attribute. If the value is true, it indicates that the method is blocked and the IE method is used.
Solution: Determine whether stopPropagation exists. If so, use the standard syntax. Otherwise, use the IE syntax.
22. Stop default events
Note: preventDefault () and returnValue () are written in standard mode, while those in IE mode are written in standard mode.
Solution: Generally, return false is recommended for blocking, but it is different from canceling the default event.

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.