Home >Web Front-end >JS Tutorial >Differences between JavaScript on IE and Firefox and alternative implementation methods_javascript skills
We often encounter some compatibility issues when dealing with js under IE and Firefox. Here are some summaries. I hope everyone will take a closer look and study
1.document.formName.item("itemName") Problem
Explanation: Under IE, you can use document.formName.item("itemName") or document.formName. elements["elementName"]; Under Firefox, you can only use document.formName.elements["elementName"].
Solution: Use document.formName.elements["elementName"] uniformly.
2. Problem with collection objects
Explanation: Under IE, you can use () or [] to obtain collection objects; under Firefox, you can only use [] to obtain collection objects.
Solution: Use [] uniformly to obtain collection class objects.
3. Custom attribute problem
Explanation: Under IE, you can use the method of obtaining regular attributes. For custom attributes, you can also use getAttribute() to obtain custom attributes; under Firefox, you can only use getAttribute() to obtain custom attributes.
Solution: Get custom attributes uniformly through getAttribute().
4.eval("idName") problem
Explanation: Under IE, you can use eval("idName") or getElementById("idName") to get the HTML whose id is idName Object; under Firefox, you can only use getElementById("idName") to obtain the HTML object with the id of idName.
Solution: Use getElementById("idName") uniformly to obtain the HTML object with the id of idName.
5. The problem that the variable name is the same as the ID of an HTML object
Explanation: Under IE, the ID of the HTML object can be used directly as the variable name of the subordinate object of the document; it cannot be used under Firefox. Under Firefox, you can use the same variable name as the HTML object ID; you cannot use IE.
Solution: Use document.getElementById("idName") instead of document.idName. It is best not to use variable names with the same HTML object ID to reduce errors; always add var when declaring variables to avoid ambiguity.
6.const problem
Explanation: Under Firefox, you can use the const keyword or the var keyword to define constants; under IE, you can only use the var keyword. Define constants.
Solution: Use the var keyword uniformly to define constants.
7. input.type attribute problem
Explanation: input.type under IE The attribute is read-only; but the input.type attribute under Firefox is read-write.
8.window.event problem
Explanation: window.event can only be used under IE Run, but cannot run under Firefox. This is because Firefox's event can only be used at the scene where the event occurs.
Solution:
IE:
...
IE&Firefox:
...
9.event.x and event.y issues
Explanation: Under IE, the even object has x, y attributes, But there are no pageX, pageY attributes; under Firefox, the even object has pageX, pageY attributes, but no x, y attributes.
Solution: use mX (mX = event.x ? event.x : event.pageX;) Replace event.x under IE or event.pageX under Firefox.
10.event.srcElement problem
Explanation: Under IE, the even object has the srcElement attribute. But there is no target attribute; under Firefox, the even object has the target attribute, but no srcElement attribute.
Solution: use obj (obj = event.srcElement ? event.srcElement : event.target;) instead of event under IE. srcElement or event.target under Firefox.
11.window.location.href problem
Explanation: Under IE or Firefox2.0.x, you can use window. location or window.location.href; under Firefox1.5.x, you can only use window.location.
Solution: Use window.location instead of window.location.href.
12 .Modal and non-modal window issues
Explanation: Under IE, modal and non-modal windows can be opened through showModalDialog and showModelessDialog; this is not possible under Firefox.
Solution: Use it directly The window.open(pageURL,name,parameters) method opens a new window. If you need to pass parameters in the child window back to the parent window, you can use window.opener in the child window to access the parent window. For example: var parWin = window.opener; parWin.document.getElementById("Aqing").value = " Aqing";
13.Frame problem
Take the following frame as an example:
(1) Access the frame object:
IE: Use window.frameId or window.frameName to access this frame object.
Firefox: You can only use window. frameName to access this frame object.
In addition, you can use window.document.getElementById("frameId") in both IE and Firefox to access this frame object.
(2) Switch frame content:
In both IE and Firefox, you can use window.document.getElementById("testFrame").src = "xxx.html" or window.frameName.location = "xxx.html" to switch the content of the frame.
If you need to pass the parameters in the frame back to the parent window, you can use parent in frme to access the parent window. For example: parent.document.form1.filename.value="Aqing";
14.body problem
Firefox’s body tag is not fully read by the browser It exists before it is entered; while IE's body must exist after the body tag is completely read by the browser.
For example:
Firefox: