Currentstyle and getcomputedstyle compatibility

Source: Internet
Author: User
Tags blank page
Currentstyle: Get the calculated style, also called the current style and final style.

Advantage: You can get the final style of the element, including the default value of the browser. Unlike style, you can only get the line style, so it is more commonly used.
Note: you cannot obtain attribute values of compound styles, such as background. You can only obtain a single style, such as background-color.

Alert (oabc. currentstyle );
Unfortunately, this amazing thing cannot be perfectly supported by various browsers. To be precise, in the browser I tested, "Object cssstyledeclaration" is displayed in IE8 and opera 11; and "undefined" is displayed in FF 12, chrome 14, and Safari 5 ".

Although currentstyle cannot be applied to all browsers, you can separate the supported and unsupported browsers Based on the test results, and then find the compatible method.

VaR oabc = Document. getelementbyid ("ABC ");
If (oabc. currentstyle ){
// IE, opera
Alert ("I support currentstyle ");
} Else {
// Ff, chrome, Safari
Alert ("I do not support currentstyle ");
In the FF browser, we can use getcomputedstyle (OBJ, false) to achieve the same effect as currentstyle in IE.

Getcomputedstyle (OBJ, false): In the new FF version, you only need the first parameter, that is, the operation object. The second parameter, "false", is also a common method, the purpose is to be compatible with Firefox browsers of earlier versions.

Compatible Syntax:

VaR oabc = Document. getelementbyid ("ABC ");
If (oabc. currentstyle ){
// IE, opera
// Alert ("I support currentstyle ");
Alert (oabc. currentstyle. width );
} Else {
// Ff, chrome, Safari
// Alert ("I do not support currentstyle ");
Alert (getcomputedstyle (oabc, false). width );
The id = "ABC" of the body in a blank page. Test the above Code. "Auto" is displayed for IE and opera, and "*** PX" is displayed for the other three browsers ". Although the results are different, it can be found that chrome and Safari are the same as Firefox, and the attribute values are successfully read. The three browsers (FF, chrome, and Safari) that do not support currentstyle support getcomputedstyle.

The results show that the browser supports currentstyle + getcomputedstyle to ensure compatibility with mainstream browsers. And compatible writing is not complex. Do you have mastered it? Pai_^

Support currentstyle: IE, opera
Supports getcomputedstyle: Firefox, chrome, Safari

Pay attention to the final pop-up content. currentstyle returns the default browser value "Auto", while getcomputedstyle returns the specific value "** PX ".

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