简体   繁体   中英

Pulling Stylesheet src/href from an external file, JQuery/Javascript

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.

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM