JavaScript solves browser compatibility issues

Source: Internet
Author: User
Tags object empty eval variables tagname variable window blank page

compatibility issues are caused by multiple browsers being present at the same time. These browsers sometimes behave differently when they are working on an identical page. This difference may be small, or even unnoticed, and may be large or even cause a browser to not navigate normally. We refer to the problem that causes these differences collectively as "browser compatibility issues." Let's look at Javascript 's approach to solving compatibility issues.

1. Document.form.item question

Problem:

A statement such as Document.formName.item ("ItemName") exists in the code and cannot be run under FF

Workaround:

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

2. Collection Class object Ask

Problem:

Many collection class objects in the code use (), IE can accept, FF can not

Workaround:

Instead, use [] as the subscript operation, for example:

Document.getelementsbyname ("InputName") (1) Changed to Document.getelementsbyname ("InputName") [1]

3. window.event

Problem:

Unable to run on FF using window.event

Workaround:

The event in FF can only be used on the spot where the incident occurred, and this issue cannot be resolved temporarily. The event can be uploaded to the function to work around it:

 
  
  
  1. OnMouseMove = "functionname (event)"
  2. function functionname (e) {
  3. e = e window.event;
  4. ......
  5. }

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

Problem:

In IE, the ID of an HTML object can be used directly as the subordinate object variable name of document, and in FF it cannot be

Workaround:

Use object variables all with standard getElementById ("Idname")

5. Obtaining an object with a idname string

Problem:

In IE, you can use eval ("Idname") to get an HTML object with an ID of idname, which in FF cannot be

Workaround:

Replace eval ("Idname") with getElementById ("Idname")

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

Problem:

In FF, because the object ID is not the name of an HTML object, you can use the same variable name as the HTML object ID, ie cannot

Workaround:

In the declaration of variables, all add var to avoid ambiguity, so that in IE also normal operation

It is best not to take variable names that are the same as HTML object IDs to reduce errors

7. Event.x and EVENT.Y issues

Problem:

In IE, the event object has a X,y attribute, and there is nothing in FF

Workaround:

In FF, the equivalent to Event.x is Event.pagex, but Event.pagex ie does not

Therefore, the use of event.clientx instead of event.x, in IE also has this variable

The subtle difference between Event.clientx and Event.pagex is the scroll bar.

To be exactly the same, you can do this:

MX = Event.x? Event.x:event.pagex;

And then use MX instead of event.x.

8. About Frame

Problem:

In IE, you can use Window.testframe to get the frame,ff.

Workaround:

 
  
  
  1. Window.top.document.getElementById ("Testframe"). src = ' xx.htm '
  2. window.top.frameName.location = ' xx.htm '

9. Get the attributes of the element

In FF, the attribute that you define must be getattribute () to obtain

10. No parentelement,parement.children in FF with Parentnode,parentnode.childnodes

Problem:

ChildNodes the meaning of the subscript is different in IE and FF, the childnodes of FF inserts a blank text node

Workaround:

You can avoid this problem by Node.getelementsbytagname ().

Problem:

When the nodes in HTML are missing, IE and FF have different explanations for parentnode, for example:

<form>

<table>

<input/>

</table>

</form>

The value of Input.parentnode in FF is form, and the value of Input.parentnode in IE is an empty node

Problem:

The node in FF itself has no Removenode method

Workaround:

You must use the following method Node.parentNode.removeChild (node)

The const question.

Problem:

You cannot use the const keyword in IE

Workaround:

Replace with Var

A. Body Object

The body of the FF body tag is not fully read by the browser before the existence, and IE must be completely read in the body after the existence of

This will occur in IE, when the document is not loaded, appendchild on the body will appear a blank page problem

Workaround:

Everything on the body insert the action of the node, all in the onload after the

URL encoding

Problem:

General FF does not recognize the & in JS

Workaround:

In JS if the write URL to write directly & do not write &

NodeName and TagName issues

Problem:

In FF, all nodes have nodename value, but Textnode has no tagName value, in IE, the use of nodename is problematic

Workaround:

Use TagName, but should detect whether it is empty

15. Element attributes

IE under the Input.type property is read-only, but can be modified under FF

Problems with Document.getelementsbyname () and Document.all[name]

Problem:

In IE, Getelementsbyname (), Document.all[name] cannot be used to obtain DIV elements

Whether there are other elements that cannot be taken is not known (the issue is debatable and is still under study)

17. Calling the elements in a child frame or other frame

In IE, you can use the following method to get the value in a child element

 
  
  
  1. document.getElementById ("FrameName"). (document.) ElementName
  2. window.frames["FrameName"].elementname

In FF, it needs to be executed in the following form, which is compatible with IE:

 
  
  
  1. window.frames["FrameName"].contentwindow.document.elementname
  2. window.frames["FrameName"].document.elementname

18. Object width and Height assignment problem

Problem:

Invalid statement in Firefox similar to obj.style.height = Imgobj.height

Workaround:

Unified use of Obj.style.height = Imgobj.height + "px";

Question of innertext

Problem:

InnerText in IE can work, but innertext in Firefox but not

Workaround:

Use textcontent instead of innertext in non-IE browsers

Event.srcelement and Event.toelement issues

Problem:

IE, the even object has a srcelement attribute, but no target attribute; Firefox, even object has target attribute, but no srcelement attribute

Workaround:

 
  
  
  1. var Source = E.target E.srcelement;
  2. var target = E.relatedtarget e.toelement;

21. Prohibit selection of Web page content

Problem:

FF need to use CSS prohibit, ie with JS prohibit

Workaround:

IE:obj.onselectstart = function () {return false;}

FF:-moz-user-select:none;

22. Capturing events

Problem:

FF no setcapture (), ReleaseCapture () method

Workaround:

Ie:

 
  
  
  1. Obj.setcapture ();
  2. Obj.releasecapture ();

Ff:

 
  
  
  1. Window.captureevents (Event.MOUSEMOVEEvent.MOUSEUP);
  2. Window.releaseevents (Event.MOUSEMOVEEvent.MOUSEUP);
  3. if (!window.captureevents)
  4. {
  5. O.setcapture ();
  6. }
  7. Else
  8. {
  9. Window.captureevents (Event.MOUSEMOVEEvent.MOUSEUP);
  10. }
  11. if (!window.captureevents)
  12. {
  13. O.releasecapture ();
  14. }
  15. Else
  16. {
  17. Window.releaseevents (Event.MOUSEMOVEEvent.MOUSEUP);
  18. }

This article is mainly about the compatibility issues in IE and FF two browsers. As browsers grow in variety, there are certainly more compatibility issues. The "resolution" of browser compatibility issues requires the efforts of browser developers, the consortium, and developers to achieve this.

"Edit Recommendation"

    1. JS script compatibility Problem Solving solution
    2. Parsing javascript compatibility Test browser Firefox usage
    3. JS script compatibility Problem Solving solution
    4. IE6, IE7, IE8 three browser compatibility comparison
"Responsible Editor: Qihappy TEL: (010) 68476606"


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.