The difference of JavaScript in IE and Firefox and the realization of mutual substitution 1th/2 page _javascript tips

Source: Internet
Author: User
Tags eval

1.document.formname.item ("ItemName") problem

Note: Under IE, you can use Document.formName.item ("ItemName") or document.formname.elements["elementname"]; Firefox, you can only use the document.formname.elements["ElementName"].
Workaround: Unify the use of document.formname.elements["ElementName"].

2. Collection Class object Problem

Note: IE, you can use () or [] get the Collection class object; Firefox, you can only use [] to get collection class objects.
WORKAROUND: Use [] to get collection class objects uniformly.

3. Custom attribute Issues

Note: Under IE, you can get the custom attribute by using the method of obtaining the General property, or you can use GetAttribute () to get the definition attribute. Firefox, you can only use getattribute () to get the definition attribute.
Workaround: The unification is obtained from the defined attribute through GetAttribute ().

4.eval ("idname") problem

Note: Under IE, you can use either eval ("Idname") or getElementById ("Idname") to obtain an HTML object with an ID of idname; Firefox can only use getElementById ("Idname") to get an HTML object with an ID of idname.
Workaround: Unify the getElementById ("Idname") to get the HTML object with the ID idname.

5. Variable name and an HTML object ID the same problem

Note: Under IE, the ID of HTML object can be used directly as the subordinate object variable name of document; Firefox is not. Firefox, you can use the same variable name as the HTML object ID, ie cannot.
Workaround: Use document.getElementById ("Idname") instead of document.idname. It is best not to take a variable name with the same HTML object ID to reduce the error; When declaring a variable, add var to avoid ambiguity.

6.const Problem

Description: Firefox, you can use the Const keyword or the var keyword to define constants, ie, only use the var keyword to define constants.
Workaround: Use the var keyword uniformly to define constants.

7.input.type Property Problem

Note: IE under the Input.type property is read-only, but Firefox under the Input.type property is read and write.

8.window.event Problem

Description: Window.event can only run in IE, but not under Firefox, this is because Firefox event can only be used in the scene of the incident occurred.
Workaround:
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 problems

Note: Under IE, the even object has x,y attribute, but there is no pagex,pagey attribute; Firefox, even objects have pagex,pagey properties, but no x,y properties.
Workaround: Use mx (mx = event.x Event.x:event.pagex) to replace the event.x under IE or the Event.pagex under Firefox.

10.event.srcelement Problem

Note: Under IE, the even object has srcelement attribute, but there is no target attribute; Firefox, the even object has the target attribute, but there is no srcelement attribute.
Workaround: Use obj (obj = event.srcelement event.srcElement:event.target) to replace the event.srcelement under IE or the Event.target under Firefox.

Current 1/2 page 12 Next read the full text
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.