0

Say I have an html source and I know the URL, "example_url.html". This html file has a <link href="style.css" rel="stylesheet"/>.

This is an iframe's src I am using in another view, and I want to pull the stylesheet listed in "example_url.html". How do I go about that?

And I don't know the actual href of stylesheet, and it changes based on various factors (but everything shares a domain name). I'll need it as a variable.

8
  • So accessing CSS of parent document inside an iframe? Commented Jun 13, 2013 at 15:39
  • Can you not get it using Firebug? Commented Jun 13, 2013 at 15:46
  • Have you tried example_url.html/style.css? Commented Jun 13, 2013 at 15:46
  • I don't know the actual stylesheet's href, "style.css" is just an example. It's much more complicated and it changes based on various things. Commented Jun 13, 2013 at 15:49
  • I'm not entirely sure what CSS of parent document means, but I am in "index.html" which loads iframe with the src "example.html", and "example.html" has the stylesheet "style.css". So the page I'm currently on doesn't have "style.css" in it's source. Sorry if this is confusing. googling Firebug Commented Jun 13, 2013 at 15:50

1 Answer 1

1

Assuming you give the iframe an ID, I think something like this should work:

var iframe = document.getElementById("the-iframe-id");
var child_doc = iframe.contentDocument || iframe.contentWindow.document;
var child_links = child_doc.getElementsByTagName("link");
var child_css;
for (var i = 0; i < child_links.length; i++) {
  if (child_links[i].rel == "stylesheet") {
      child_css = child_links[i].href;
      break;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hmmmm this seems like it should work. I grab the iframe successfully, but then child_doc returns (null || undefined) for those two possible assignments. It does have the src set though.
Oh you know what. I am being so silly. I apologize. Normally this does have the same domain, but I"m on local host. Duh. I bet this will work otherwise. Thanks!
Yes, that's the cross-domain failure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.