Home > Web Front-end > CSS Tutorial > How Can I Retrieve Computed Styles from a Cross-Domain Iframe?

How Can I Retrieve Computed Styles from a Cross-Domain Iframe?

Barbara Streisand
Release: 2024-11-30 03:25:21
Original
381 people have browsed it

How Can I Retrieve Computed Styles from a Cross-Domain Iframe?

Retrieve Computed Style from Cross-Domain Iframe

This article focuses on obtaining the computed style of an HTML element within an iframe located on a different domain. The goal is to retrieve specific CSS properties like height and width that browsers calculate and use.

To access the computed style, you can utilize the following:

Browsers Other Than IE

window.getComputedStyle(document.getElementById("frameId"), null).getPropertyValue("height")
Copy after login

This script returns the computed height of the element. Alternatively, you can also use:

window.getComputedStyle(document.getElementById("frameId"), null).getPropertyCSSValue("height").cssText
Copy after login

IE

element.currentStyle
Copy after login

This method returns the current style object for the specified element.

Cross-Domain Considerations

Since you cannot access the iframe directly due to cross-domain restrictions, you need to find another way to access the element you need. You can do this by manipulating the DOM of the iframe's document.

Selecting the Target Element

To select the desired element, you can use the following:

document.getElementById("frameId").contentDocument.getElementById("brshtml")
Copy after login

This assumes that the head element of the iframe's document has an id of "brshtml."

The above is the detailed content of How Can I Retrieve Computed Styles from a Cross-Domain Iframe?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template