Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the compatibility problem of JS in IE and Firefox

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

JS in IE and Firefox compatibility problems and solutions are how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

If you are familiar with JS's solution to IE and Firefox compatibility problems, let me share it with you. For example, there are many statements like document.formName.item ("itemName") in existing code that cannot be run under MF. We can use document.formName.elements ["elementName"] to solve the problem instead.

Compilation of JS compatibility issues in IE and Firefox

IE instead of InternetExplorer and MF instead of MozzilaFirefox

1.document.form.item problem

(1) existing problems:

There are many statements like document.formName.item ("itemName") in the existing code that cannot be run under MF

(2) solution:

Use document.formName.elements ["elementName"] instead

(3) other

two。 Set class object problem

(1) existing problems:

Many collection class objects in existing code are accessed using (), which is acceptable to IE and not to MF.

(2) solution:

Use [] as the subscript operation instead. For example, document.forms ("formName") is changed to document.forms ["formName"].

For example, document.getElementsByName ("inputName") (1) is changed to document.getElementsByName ("inputName") [1]

(3) other

3.window.event

(1) existing problems:

Cannot run on MF using window.event

(2) solution:

MF's event can only be used at the scene of the incident, and this problem cannot be resolved for the time being. You can change it like this:

Original code (can be run in IE):

... FunctiongotoSubmit (evt) {evtevt=evt?evt: (window.event?window.event:null);... Alert (evt); / / useevt.}

In addition, if the * * line in the new code remains the same as the old code (that is, the gotoSubmit call has no arguments), it can still only be run in IE without error. Therefore, the tpl part of this scheme is still compatible with the old code.

The problem of using the id of the 4.HTML object as the object name

(1) existing problems

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

(2) solution

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

5. The problem of getting objects with idName strings

(1) existing problems

In IE, a HTML object whose id is idName can be obtained by using eval (idName), but not in MF.

(2) solution

Replace eval (idName) with getElementById (idName).

6. The problem that the variable name is the same as a HTML object id

(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, but not in IE.

(2) solution

When declaring variables, always add var to avoid ambiguity, so that it can work properly in IE.

In addition, * do not take the same variable name as the HTML object id to reduce errors.

(3) other

7.event.x and event.y problem

(1) existing problems

In IE, the event object has the xrecoy attribute, which is not in MF.

(2) solution

In MF, the equivalent of event.x is event.pageX. But not in event.pageXIE.

Therefore, event.clientX is used instead of event.x. This variable is also available in IE.

There are subtle differences between event.clientX and event.pageX (when the entire page has scroll bars), but most of the time they are equivalent.

If you want to be exactly the same, you can be a little more troublesome: mX=event.x?event.x:event.pageX; and then use mX instead of event.x

(3) other

Event.layerX exists in both IE and MF, but whether the specific meaning is different has not been tested.

8. About frame

(1) existing problems

You can use window.testFrame to get the frame,mf in IE.

(2) solution

The main differences between mf and ie in the use of frame are:

If the following attributes are written in the frame tag:

Then ie can access the window object corresponding to the frame through id or name, while mf can only access the window object corresponding to the frame through name. For example, if the above frame tag is written in the htm in the top window, you can access it like this

Ie:window.top.frameId or window.top.frameName to access the window object

Mf: this is the only way to access this window object by window.top.frameName

In addition, you can use window.top.document.getElementById ("frameId") to access frame tags in both mf and ie, and you can switch the content of frame through window.top.document.getElementById ("testFrame"). Src='xx.htm', and you can also switch the content of frame through window.top.frameName.location='xx.htm'.

9. In mf, self-defined properties must be obtained by getAttribute ()

10. There is no parentElementparement.children in mf.

ParentNodeparentNode.childNodes

The meaning of the subscript of childNodes is different in IE and MF. MF uses the DOM specification, and blank text nodes are inserted in childNodes.

You can generally avoid this problem through node.getElementsByTagName ().

IE and MF have different interpretations of parentNode when nodes are missing in html, for example

The value of input [XSS _ clean] in MF is form, while the value of input [XSS _ clean] in IE is empty node.

The node in MF does not have a removeNode method, so you must use the following method node [XSS _ clean] .removeChild (node)

11.const problem

(1) existing problems:

The const keyword cannot be used in IE. Such as constconstVar=32; in IE, this is a syntax error.

(2) solution:

Do not use const, use var instead.

12.body object

MF's body exists before the body tag is fully read by the browser, while IE can only exist after the body is fully read.

13.urlencoding

If you write url in js, write directly & don't write & for example, varurl='xx.jsp?objectName=xx&objectEvent=xxx'

Frm.action=url, it is very likely that url will not be displayed properly so that the parameters are not passed to the server correctly.

Generally speaking, the error parameter of the server was not found.

Of course, if it is an exception in tpl, because tpl conforms to the xml specification, it is required to be written as &

In general, MF does not recognize & in js

14.nodeName and tagName problem

(1) existing problems:

In MF, all nodes have a nodename value, but textNode has no tagname value. In IE, there seems to be a problem with the use of nodeName.

(2) solution:

Use tagName, but check to see if it is empty.

15. Element attribute

The input.type attribute is read-only under IE, but can be modified under MF.

The question of 16.document.getElementsByName () and document.all [name]

(1) existing problems:

In IE, neither getElementsByName () nor document.all [name] can be used to get div elements (it is not known whether there are other elements that cannot be fetched).

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report