JavaScript in IE and Firefox browser 7 different compatibility writing summary _javascript tips

Source: Internet
Author: User
In this article, the author describes the differences between 7 JavaScript in IE and Firefox.
1. CSS "Float" value
The most basic syntax for accessing a given CSS value is: Object.style.property, using the hump to replace a value with a connector, for example, to access a <div> Background-color value with the id "header". We use the following syntax:

document.getElementById ("header"). style.backgroundcolor= "#ccc";
But since the word "float" is a JavaScript reserved word, we can't access it with Object.style.float, where we can do this in two browsers:
Write this in IE:

document.getElementById ("header"). Style.stylefloat = "Left";
Write this in Firefox:

document.getElementById ("header"). Style.cssfloat = "Left";

2. The projection style of the elements
JavaScript can use the Object.style.property syntax to easily access and modify a CSS style externally, but the limitation is that the syntax can only take out a set of inline styles or a style set directly by JavaScript. Does not have access to an external style sheet. To access the "extrapolated" style of an element, we can use the following code:
Write this in IE:

var myObject = document.getElementById ("header");
var myStyle = MyObject.currentStyle.backgroundColor;
Write this in Firefox:

var myObject = document.getElementById ("header");
var Mycomputedstyle = Document.defaultView.getComputedStyle (myObject, NULL);
var myStyle = Mycomputedstyle.backgroundcolor;
3. Accessing the element's "class"
Like "float", "class" is a reserved word for JavaScript, in which we use the syntax below to access "class".
Write this in IE:

var myObject = document.getElementById ("header");
var myattribute = Myobject.getattribute ("ClassName");
Write this in Firefox:

var myObject = document.getElementById ("header");
var myattribute = Myobject.getattribute ("class");
This syntax would also apply using the SetAttribute method.

4. Access <label> tags "for"
As mentioned in the 3rd, we also need to use the syntactic distinction to access the "for" in <label> Tags:
Write this in IE:

var myObject = document.getElementById ("MyLabel");
var myattribute = Myobject.getattribute ("htmlfor");
Write this in Firefox:

var = document.getElementById ("MyLabel");
var myattribute = Myobject.getattribute ("for");
5. Get the position of the mouse pointer
It may be very rare for you to calculate the position of the mouse pointer, but the syntax in IE and Firefox is different when you need it. The code written here will be the most basic, or it may be a part of a complex event processing. But they can explain the similarities and differences. At the same time, it must be pointed out that the results are more different than the Firefox,ie, this method itself is a bug. Typically, this difference can be compensated with a "drag position", but that's another theme of the article:!
Write this in IE:

var mycursorposition = [0, 0];
Mycursorposition[0] = Event.clientx;
MYCURSORPOSITION[1] = Event.clienty;
Write this in Firefox:

var mycursorposition = [0, 0];
Mycursorposition[0] = Event.pagex;
MYCURSORPOSITION[1] = Event.pagey;
6. Get the visible area, the size of the window
Sometimes we need to find the size of the visual location of the browser, which we usually call "visible areas."
Write this in IE:

var mybrowsersize = [0, 0];
Mybrowsersize[0] = document.documentElement.clientWidth;
MYBROWSERSIZE[1] = document.documentElement.clientHeight;
Write this in Firefox:

var mybrowsersize = [0, 0];
Mybrowsersize[0] = window.innerwidth;
MYBROWSERSIZE[1] = window.innerheight;
7. Alpha Transparent
Well, it's not a JavaScript syntax problem, but an alpha transparency from CSS. But when an object needs to fade in/out it needs JavaScript to perform, which is done by accessing the CSS's alpha transparency settings, usually in a loop. You need to modify the JavaScript code as follows:
Write this in IE:

#myElement {filter:alpha (opacity=50);}
Write this in Firefox:

#myElement {opacity:0.5;}
Write this in IE:

var myObject = document.getElementById ("myelement");
MyObject.style.filter = "Alpha (opacity=80)";
Write this in Firefox:

var myObject = document.getElementById ("myelement"); MyObject.style.opacity = "0.5";
What difference does it have?
This is the author (via:7 JavaScript differences Between Firefox & IE) Individuals based on their own experience, and Sofish is still a novice in JavaScript. If there are more differences, you are welcome to put forward to share it out and learn together!
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.