How to Add CSS to iFrames from a Different Domain?

Patricia Arquette
Release: 2024-10-25 05:38:02
Original
215 people have browsed it

How to Add CSS to iFrames from a Different Domain?

Adding CSS to iFrames

In cases where iframes are loaded from a different domain, applying CSS directly can be a challenge. The following solution addresses this issue.

Modifying Style Sheets

Based on solutions, here are methods to add CSS:

Using JavaScript:

<code class="js">var cssLink = document.createElement("link");
cssLink.href = "file://path/to/style.css";
cssLink.rel = "stylesheet";
cssLink.type = "text/css";
frames['iframe'].document.body.appendChild(cssLink);</code>
Copy after login

Using jQuery:

<code class="js">var $head = $("iframe").contents().find("head");
$head.append($("<link/>", { rel: "stylesheet", href: "file://path/to/style.css", type: "text/css" }));</code>
Copy after login

Potential Security Concerns

Note that security concerns may arise when disabling the same-origin policy in Safari. Appropriate measures should be taken to ensure secure implementation.

The above is the detailed content of How to Add CSS to iFrames from a Different Domain?. 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