Differences between Javascript in IE and FireFox

Source: Internet
Author: User

1.doc ument. formName. item ("itemName ")
Description: 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"].
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.
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.
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 event object has the srcElement attribute, but does not have the target attribute. In Firefox, the event 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.
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:
Copy codeThe Code is as follows:
<Body>
<Script type = "text/javascript">
Document. body. onclick = function (evt ){
Evt = evt | window. event;
Alert (evt );
}
</Script>
</Body>

IE & Firefox:
Copy codeThe Code is as follows:
<Body>
</Body>
<Script type = "text/javascript">
Document. body. onclick = function (evt ){
Evt = evt | 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:
Copy codeThe Code is as follows:
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. innerText works normally in IE, but innerText does not work in FireFox.
Solution:
Copy codeThe Code is as follows:
If (navigator. appName. indexOf ("Explorer")>-1 ){
Document. getElementById ('element'). innerText = "my text ";
} Else {
Document. getElementById ('element'). textContent = "my text ";
}

18. The statement similar to obj. style. height = imgObj. height in FireFox is invalid.
Solution:
Copy codeThe Code is as follows:
Obj. style. height = imgObj. height + 'px ';

19. 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:
Copy codeThe Code is as follows:
// 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 );

20. 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;
21. 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.
22. Transparent CSS
IE: filter: progid: DXImageTransform. Microsoft. Alpha (style = 0, opacity = 60 ).
FF: opacity: 0.6.
23. 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 ;.
24. 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;
25. ie supports document. all, but firefox does not.
Replace document. all with one of the following three tags.
GetElementsByTagName ("tagName") can be used to obtain a set of all tag elements.
GetElementById ("idName") obtains an element by id.
GetElementsByName ("Name") can get an element by name attribute
26. How to Use innerHTML in firefox
Copy codeThe Code is as follows:
<Div id = "online"> </div>
Document. all. online. innerHTML; // This method can be used in IE, but it is not a standard method.
Document. getElementById ("online"). innerHTML; // you can use innerHTML in firefox.

27w.eval(example and window.exe cScript () run the script
Ie、fireroxboth support eval((, firefoxdoes not support window.exe cScript ()
Solution: unified use of eval ()
28. Rewrite event processing functions
Solution: (example): for example, rewrite onclick () of the document and use document. onclick = function (){...}

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.