Compatibility issues and solutions for multiple browsers in Javascript

Source: Internet
Author: User

I. document. formName. item ("itemName ")

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

Ii. Collection objects

Problem description: 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.

Iii. Custom Attributes

Problem description: 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: Get custom attributes through getAttribute.

Iv. eval ("idName") Problems

Problem description: 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.

Problem description: in IE, the ID of the HTML object can be used directly as the variable name of the subordinate object of the document, but not in Firefox. In Firefox, the variable name that is the same as the ID of the HTML object can be used, IE.
Solution: Use document. getElementById ("idName") to replace document. idName. We recommend that you do not use variable names with the same HTML Object ID to reduce errors. When declaring variables, add the var keyword to avoid ambiguity.

Vi. const Problems

Problem description: 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.

VII. input. type attributes

Problem description: The input. type attribute in IE is read-only, but the input. type attribute in Firefox is read/write.
Solution: do not modify the input. type attribute. If you must modify it, You can first hide the original input, and then insert a new input element at the same position.

8. window. event Problems

Problem description: window. event can only be run in IE, but not in Firefox, because Firefox event can only be used in the event.
Solution: add the event parameter to the function where the event occurs and use var myEvent = evt? Evt :( window. event? Window. event: null)
Example:

<Input type = "button" onclick = "doSomething (event)"/>
<Script language = "javascript">
Function doSomething (evt ){
Var myEvent = evt? Evt :( window. event? Window. event: null)
...
}
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: var myX = event. x? Event. x: event. pageX; var myY = event. y? Event. y: event. pageY;
If you consider 8th issues, use myEvent instead of event.

10. event. srcElement Problems

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 srcObj = event. srcElement? Event. srcElement: event.tar get;
If you consider 8th issues, use myEvent instead of event.

11. window. location. href

Problem description: 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. Of course, you can also consider using the location. replace () method.

12. Modal and non-modal window Problems

Problem description: in IE, you can open modal and non-modal windows through showModalDialog and showModelessDialog; in Firefox, you cannot.
Solution: 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. opener in the Child window to access the parent window. If you need a parent window to control the child window, use var subWindow = window. open (pageURL, name, parameters); to obtain the new window object.

13. frame and iframe Problems

The following frame is used as an example:

(1) access the frame object
IE: Use window. frameId or window. frameName to access this frame object;
Firefox: Use window. frameName to access this frame object;
Solution: You can access the frame object by using the metadata Doc ument. getElementById ("frameId;
(2) Switching frame content
In both IE and Firefox, you can use javasdomaindoc ument. getElementById ("frameId"). src = javaswebjx.com.html "or window. frameName. location = javaswebjx.com.html" to switch the frame content;
If you want to return the parameters in the frame to the parent window, you can use the parent keyword in the frame to access the parent window.

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.