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

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

Barbara Streisand
Release: 2024-12-05 04:05:16
Original
521 people have browsed it

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

How to Retrieve Computed Styles from Cross-Domain Content

Problem:

Accessing the computed height and width values of an element from a cross-domain iframe can be challenging, particularly if you're not able to access the iframe's content directly.

Solution:

Using Browser-Specific Functions:

  • Firefox, Opera, Safari: window.getComputedStyle(element)
  • IE: element.currentStyle

Applying to Cross-Domain Content:

To retrieve computed styles from cross-domain content, you can use window.getComputedStyle in modern browsers or element.currentStyle in IE. However, these functions only work on elements within the current document, so you need to navigate into the iframe's DOM before using them.

Example (WebKit):

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

Native jQuery Function:

Alternatively, you can use jQuery's .height() function, which simplifies the process:

$('#frameId').height();
Copy after login

Additional Notes:

  • It's important to note that window.getComputedStyle returns an immutable object with the computed styles, while element.currentStyle returns a live object that can change as the element's style changes.
  • You may need to add an ID to the head element within the iframe to make it easier to select later on.

The above is the detailed content of How Can I Get 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