Differences between Javascript in IE and Firefox (excerpt)

Source: Internet
Author: User
1. Document. Form. Item Problems
(1) existing problems:
Many statements such as document. formname. Item ("itemname") exist in the existing code and cannot be run in Firefox.
(2) solution:
Use document. formname. elements ["elementname"]
(3) Others
See 2

2. Collection class Object Problems
(1) existing problems:
In the existing Code, many collection class objects are used (), which is acceptable to IE and cannot be used by Firefox.
(2) solution:
Use [] as the subscript operation. For example, change document. Forms ("formname") to document. Forms ["formname"].
For example, change document. getelementsbyname ("inputname") (1) to document. getelementsbyname ("inputname") [1]
(3) Others

3. Window. Event
(1) existing problems:
Window. event cannot be run on Firefox
(2) solution:
The event of Firefox can only be used in the event. This problem cannot be solved for the moment. This can be changed as follows:
Original code (run in IE): <br/> <input type = "button" name = "somebutton" value = "Submit" onclick = "javascript: gotosubmit () "/> <br/>... <br/> <script language = "JavaScript"> <br/> function gotosubmit () {<br/>... <br/> alert (window. event); // use window. event <br/>... <br/>}< br/> </SCRIPT>

New Code (run in IE and Firefox): <input type = "button" name = "somebutton" value = "Submit" onclick = "javascript: gotosubmit (Event) "/> <br/>... <br/> <script language = "JavaScript"> <br/> function gotosubmit (EVT) {<br/> EVT = EVT? EVT: (window. event? Window. event: NULL); <br/>... <br/> alert (EVT); // use EVT <br/>... <br/>}< br/> </SCRIPT>
In addition, if the first line of the new Code is not modified, it is the same as the old code (that is, the gotosubmit call does not provide a parameter), it can only be run in IE, but no error occurs. Therefore, the TPL part of this solution is still compatible with the old code.

4. Question about the ID of an HTML object as the object name
(1) Existing Problems
In ie, the ID of the HTML object can be directly used as the variable name of the subordinate object of the document. Not in Firefox.
(2) Solution
Use getelementbyid ("idname") instead of idname as the object variable.

5. The problem of getting an object using the idname string
(1) Existing Problems
In IE, Eval (idname) can be used to obtain the HTML object with ID as idname, which cannot be used in Firefox.
(2) Solution
Use getelementbyid (idname) instead of eval (idname ).

6. The variable name is the same as the ID of an HTML object.
(1) Existing Problems
In Firefox, because the Object ID is not the name of the HTML object, you can use the same variable name as the HTML Object ID, which can be in IE.
(2) Solution
When declaring variables, add VaR to avoid ambiguity, so that it can run normally in IE.
In addition, it is best not to take the same variable name as the HTML Object ID to reduce errors.
(3) Others
See question 4

7. event. X and event. Y
(1) Existing Problems
In ie, the event object has the X and Y attributes, and Firefox does not.
(2) Solution
In Firefox, event. X is equivalent to event. pagex. But event. pagex IE does not.
Therefore, event. clientx is used instead of event. X. This variable is also available in IE.
Event. clientx is slightly different from event. pagex (when the page has a scroll bar), but most of the time it is equivalent.

If it is the same, it may be a little troublesome:
MX = event. X? Event. X: event. pagex;
Use MX instead of event. x
(3) Others
Event. layerx is available in both IE and Firefox. There is no difference in the specific significance of this feature.

8. About Frame
(1) Existing Problems
In IE, you can use window. testframe to obtain the frame, but not in Firefox.
(2) Solution
The main difference between Firefox and IE in frame usage is:
If the following attributes are written in the frame tag:
<Frame src = "xx.htm" id = "frameid" name = "framename"/>
Then Ie can access the window object corresponding to this frame through ID or name.
Firefox can only access the window object corresponding to this frame through name.
For example, if the above frame label is written in the HTM in the top window, you can access
IE: window. Top. frameid or window. Top. framename to access this window object
Firefox: only window. Top. framename can access this window object.

In addition, you can use tranquility top.doc ument. getelementbyid ("frameid") to access the frame tag in Firefox and ie.
In addition, you can switch the frame content through the parameter top.doc ument. getelementbyid ("testframe"). src = 'xx.htm '.
You can also switch the frame content through window. Top. framename. Location = 'xx.htm '.
For details about frame and window, see the 'window and framework' article in BBS.
And the tests under the/test/JS/test_frame/directory
---- Adun 2004.12.09 Modification

9. In Firefox, the property defined by myself must be obtained by getattribute ().

 

10. If parentelement parement. Children is not found in Firefox, parentnode. childnodes is used.
The meanings of childnodes are different from those of Firefox. Firefox inserts blank text nodes in childnodes using Dom specifications.
You can avoid this problem by using node. getelementsbytagname.
When the HTML node is missing, ie and Firefox have different interpretations of parentnode, for example
<Form>
<Table>
<Input/>
</Table>
</Form>
In Firefox, the value of input. parentnode is form, while that of input. parentnode in IE is empty.

The node in Firefox does not have the removenode method. You must use the following method: node. parentnode. removechild (node)

11. Const Problems
(1) existing problems:
You cannot use the const keyword in IE. For example, const constvar = 32; in IE, This Is A syntax error.
(2) solution:
Use VaR instead of Const.

12. Body object
Firefox's body exists before the body label is fully read by the browser, While IE exists only after the body is fully read.

13. url Encoding
In JS, if the URL is written, write directly & do not write & amp; for example, VAR url = 'xx. jsp? Objectname = XX & amp; objectevent = XXX ';
FRM. Action = URL, so it is very likely that the URL will not be properly displayed, so that the parameter is not correctly transmitted to the server.
Generally, the server reports that the error parameter is not found.
Of course, if it is an exception in TPL, because TPL complies with the XML specification and requires & writes as & amp;
Firefox cannot recognize & amp; in JS;

14. nodename and tagname Problems
(1) existing problems:
In Firefox, all nodes have a nodename value, but textnode does not have a tagname value. In IE, nodename usage seems to be
There is a problem (the test is not performed, but my IE has been killed several times ).
(2) solution:
Use tagname, but check whether it is empty.

15. Element attributes
The input. Type attribute in IE is read-only, but can be modified in Firefox.

16. Document. getelementsbyname () and document. All [name] Problems
(1) existing problems:

In IE, neither getelementsbyname () nor document. All [name] can be used to obtain the DIV element (whether there are other elements that cannot be retrieved is unknown ).

 

17 .. http://www.websbook.com/javascript/hhhIEzjd7gJavaScriptcy_15037.html

 

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.