16 problems with JavaScript code in browser IE and Firefox compatibility

Source: Internet
Author: User
Tags eval

Following IE instead of Internet Explorer, MF replaces Mozzila Firefox

1. Document.form.item question

(1) Existing problems:

There are many Document.formName.item ("itemname") statements in existing code that cannot be run under MF

(2) Solution:

Switch to document.formname.elements["ElementName"]

(3) Other

See also 2

2. Collection Class object problem

(1) Existing problems:

Many collection class objects in existing code use (), IE can accept, MF cannot.

(2) Solution:

Use [] as the subscript operation instead. For example: Document.forms ("FormName") changed to document.forms["FormName"].

Also as follows: Document.getelementsbyname ("InputName") (1) Changed to Document.getelementsbyname ("InputName") [1]

(3) Other

3. window.event

(1) Existing problems:

Unable to run on MF using window.event

(2) Solution:

MF event can only be used at the scene of the incident, this problem can not be resolved. You can do this:

Original code (can run in IE):

      <input type="button" name="someButton" value="提交" onclick="javascript:gotoSubmit()"/>
       ...
       <script language="javascript">
         function gotoSubmit() {
           ...
           alert(window.event);  // use window.event
           ...
         }
       </script>

New code (can run in IE and MF):

      <input type="button" name="someButton" value="提交" onclick="javascript:gotoSubmit(event)"/>
       ...
       <script language="javascript">
         function gotoSubmit(evt) {
           evt = evt ? evt : (window.event ? window.event : null);
           ...
           alert(evt);       // use evt
           ...
         }
       </script>

In addition, if the first line in the new code does not change, the same as the old code (that is, the gotosubmit call does not give the parameter), it still can only run in IE, but not error. Therefore, the TPL part of this scheme is still compatible with the old code.

4. The ID of an HTML object as an object name problem

(1) Existing problems

In IE, the ID of an HTML object can be used directly as the subordinate object variable name of document. Not in MF.

(2) Solving method

Use getElementById ("Idname") instead of Idname as an object variable.

5. Obtaining an object with a idname string

(1) Existing problems

In IE, you can use Eval (idname) to obtain an HTML object with an ID of idname, which cannot be in MF.

(2) Solving method

Replace eval (idname) with getElementById (Idname).

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

(1) Existing problems

In MF, because the object ID is not the name of an HTML object, you can use the same variable name as the HTML object ID, which is not available in IE.

(2) Solving method

In the declaration of variables, all add Var, to avoid ambiguity, so that in IE can also be normal operation.

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.