JS scripting practices compatible with IE and FF

Source: Internet
Author: User
[Reading] BS development will inevitably use JavaScript, and each browser for JavaScript support is different. This requires our programmers to be compatible with them/* below IE instead of Internet Explorer to Mf/ff instead of Mozzila Firefox * *



Window.event

IE: There are window.event objects

FF: No Window.event object. You can pass an event object to a parameter of a function. such as OnMouseMove = Domousemove (event)

Workaround: var event = Event | | window.event;

Example

< script >

function test (event) {

var event = Event | | window.event;

Do something

}

</script>

< input type = "button" value = "click" onclick = "Test (Event)"/>





Mouse Current coordinates

Ie:event.x and Event.y.

FF:event.pageX and Event.pagey.

General: Both have Event.clientx and Event.clienty attributes.



The current coordinates of the mouse (plus the distance the scroll bar rolls over)

IE:event.offsetX and Event.offsety.

FF:event.layerX and Event.layery.

Workaround:

< script >

function test (event) {

var event = Event | | window.event;

or var event = event? event:window.event;//can be in this 2, or you can use if else (this shorthand)

var x = Event.offsetx | | Event.layerx;

var y = event.offsety | | Event.layery;

Do something

}

</script>

< div onmousedown = "Test (event)" ></div>

* * Other compatible solutions are similar, no more than one by one examples * * * *



Event.srcelement problem

Note: Under IE, the event object has srcelement attribute, but there is no target attribute; Firefox, the even object has the target attribute,

However, there is no srcelement attribute.

Workaround: Use obj (obj = event.srcelement?) Event.srcElement:event.target;)

To replace the event.srcelement under IE or

Firefox under the Event.target. Please also note the compatibility issue of event.



Event.toelement problem

Problem:

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

Firefox, the even object has the target attribute, but there is no srcelement property

Workaround:

var target = E.relatedtarget | | E.toelement;



Coordinates of the x and Y of the label: Style.posleft and Style.postop

IE: there is.

FF: No.

General: Object.offsetleft and Object.offsettop.



Height and width of the form

IE:document.body.offsetWidth and Document.body.offsetHeight. Note: The page must have a body tag at this point.

FF:window.innerWidth and Window.innerhegiht,

As well as Document.documentElement.clientWidth and document.documentElement.clientHeight.

General: Document.body.clientWidth and Document.body.clientHeight.



Adding events

IE:element.attachEvent ("onclick", function);.

FF:element.addEventListener ("Click", Function, True).

Generic: Element.onclick = function. Although you can use the OnClick event, the effect of the onclick and the above two methods is not the same,

OnClick only executes a process, and attachevent and AddEventListener execute a list of processes, that is, multiple processes.

For example: Element.attachevent ("onclick", func1);

Element.attachevent ("onclick", Func2) so func1 and Func2 will be executed.



Custom Properties for labels

IE: If a property value is defined for the label DIV1, the value can be obtained Div1.value and div1["value".

FF: cannot be taken with Div1.value and div1["value".

General: Div1.getattribute ("value").



Document.form.item problem

IE: Existing problem: There are many Document.formName.item ("itemname") statements in existing code that cannot be run under MF

ff/ie:document.formname.elements["ElementName"]



Set/Array Class object problem

(1) Existing problems:

Many collection class objects in existing code use (), IE can accept, MF cannot.

(2) Solution:

Use [] as the subscript operation instead. For example: Document.forms ("FormName") changed to document.forms["FormName"].

Also as follows: Document.getelementsbyname ("InputName") (1) Changed to Document.getelementsbyname ("InputName") [1]



An issue with the ID of an HTML object as an object name

(1) Existing problems

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

(

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.