IE, FF, Chrome browser in the JS difference Introduction _ Basic knowledge

Source: Internet
Author: User
Tags reserved
Because the browser companies for their own benefit, so far the browser's HTML standards or JS standards have not been unified. In the common development, we often use the JS framework has basically helped us to deal with JS in each browser differences, but as a developer, there is a need to understand the differences in the browser JS.

FF, Chrome: No window.event objects
FF, Chrome: No Window.event object, only event object, IE only support window.event, and other mainstream browsers both support, so generally written: function handle (e) {e = e | | event;}

Getting HTML elements
IE: Support El.name, El.getattribute (name)
FF, Chrome: Basic Properties Support El.name the remaining properties support only El.getattribute (name)

Custom Attribute Issues
IE, you can obtain custom attributes by using the method that gets the general properties, or you can use GetAttribute () to get the defined attributes, and Firefox, you can only use getattribute () to derive the defined attributes.

Ajax Request
Ie:new ActiveXObject ()
FF, Chrome:new XMLHttpRequest ()

Getting HTML elements
IE: Support El.name, El.getattribute (name)
FF, Chrome: Basic Properties Support El.name the remaining properties support only El.getattribute (name)

the use of innertext
FF does not support innertext, it supports textcontent to implement innertext, but Textcontent does not consider the element display way like innertext, so it is not completely compatible with IE. If you do not use Textcontent, the string contains no HTML code and can be replaced with innerHTML.
if (document.all) {
document.getElementById (' element '). innertext = "MyText";
} else{
document.getElementById (' element '). Textcontent = "MyText";
}

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.
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;

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;

Alpha Transparent
This is not a JavaScript syntax problem, but is derived from the CSS alpha transparency. 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. The JavaScript code you need to modify is 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";

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";

The inferred style of the element
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;

accessing the element's "class"
"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");
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.