Avatar of hankknight
hankknight
Flag for Canada asked on

Append CSS to iframe

I want to be able to change the CSS used in an iframe from a link.

My iframe does not have a name, only an id.

My code below does NOT work.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
<script type="text/javascript">
function addEditorCSS (child,css) {
  var ifr = getElementById(child);
  var headID = ifr.document.getElementsByTagName("head")[0];
  var cssNode = ifr.document.createElement("link");
  cssNode.type = "text/css";
  cssNode.rel = "stylesheet";
  cssNode.href = css;
  cssNode.media = "screen";
  headID.appendChild(cssNode);
}
</script>
</head>
<body>
<h1>Hello World</h1>
<iframe src="2.html" id="elm1_ifr"></iframe>
<hr />
<a href="javascript:('elm1_ifr','http://fds2432.googlepages.com/red.css')">Change CSS</a>
</body>
</html>

Open in new window

JavaScript

Avatar of undefined
Last Comment
hankknight

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
quincydude

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Michel Plungjan

FF and IE
Note I change ID to NAME and fixed the href of the clicked link

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
<script type="text/javascript">
function addEditorCSS (child,css) {
  var ifr = window.frames[child];
  var headID = ifr.document.getElementsByTagName("head")[0];
  var cssNode = ifr.document.createElement("link");
  cssNode.type = "text/css";
  cssNode.rel = "stylesheet";
  cssNode.href = css;
  cssNode.media = "screen";
  headID.appendChild(cssNode);
}
</script>
</head>
<body>
<h1>Hello World</h1>
<iframe src="2.html" name="elm1_ifr"></iframe>
<hr />
<a href="#" onClick="addEditorCSS('elm1_ifr','http://fds2432.googlepages.com/red.css'); return false">Change CSS</a>
</body>
</html>

Open in new window

hankknight

ASKER
Thanks!

mplungjan, your code will not work for me because as I mentioned in the question, my iframe does NOT have a name, only an id.

quincydude, your code works great.
Michel Plungjan

var ifr = window.frames[0];

will work for a nameless frame where 0 is the first frame and subsequent frames are numbered 1 and up
but the solution given works in IE and FF
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
hankknight

ASKER
I have run into trouble implamenting this concept into TinyMCE.

I have asked a related question here:
https://www.experts-exchange.com/questions/24249167/Append-CSS-to-TinyMCE-iframe.html