Compilation of Javascript IE and Firefox compatibility

Source: Internet
Author: User

Internet Explorer is replaced by IE and Mozzila Firefox is replaced by MF.

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 MF.
(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 MF.
(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 MF.
(2) solution:
MF events can only be used in the event. This problem cannot be solved. This can be changed as follows:
Original code (can be run in IE ):
<Input type = "button" name = "someButton" value = "Submit" onclick = "javascript: gotoSubmit ()"/>
...
<Script language = "javascript">
Function gotoSubmit (){
...
Alert (window. event); // use window. event
...
}
</Script>

New Code (run in IE and MF ):
<Input type = "button" name = "someButton" value = "Submit" 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 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. It cannot be in MF.
(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 MF.
(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 MF, 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 cannot be used 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 MF does not.
(2) Solution
In MF, 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 MF. There is no difference in the specific significance of this function.


8. About frame
(1) Existing Problems
In IE, the frame can be obtained using window. testFrame, but not in mf.
(2) Solution
The main difference between mf 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.
However, mf 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
Mf: only window. top. frameName can access this window object.

In addition, both mfand iecan use javasdesktop.doc ument. getElementById ("frameId") to access the frame tag.
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 mf, attributes defined by myself must be obtained by getAttribute ().
10. If there is no parentElement parement. children in mf, use
ParentNode. childNodes
ChildNodes has different meanings in IE and MF. MF uses DOM specifications, and blank text nodes are inserted in childNodes.
You can avoid this problem by using node. getElementsByTagName.
When a node in html is missing, IE and MF have different interpretations of parentNode, for example
<Form>
<Table>
<Input/>
</Table>
</Form>
In MF, the value of input. parentNode is form, while that of input. parentNode in IE is empty.

The node in MF 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
The MF body exists before the body tag 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 & for example, var url = 'xx. jsp? ObjectName = xx & 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, the requirements are as follows &
Generally, MF cannot recognize &


14. nodeName and tagName Problems
(1) existing problems:
In MF, 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 MF.


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 ).

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.