Jquery inserting all stylesheets in iframe

How can I insert all the stylesheets of the parent window into the iframe (samedomain) head?

My code based on a similar question:

function () { var d = frames[0].document; var stylesheets = $("link").outerhtml; d.open(); d.write( '<html><head>'+ stylesheets + '<style type="text/css">'+ '<\/style><\/head><body><\/body><\/html>' ); d.close(); } 

Obviously this does not work outside of IE. Thanks in advance.

Edit: an attempt based on Anthony's answer:

  $("link[type='text/css']").each(function() { var stylesheet = $(this).clone(); $("iframe").contents().find("head").append(stylesheet); }); 
+6
javascript jquery iframe wysiwyg
source share
2 answers

The problem with the selected answer is that it uses .html() , which returns only the internal html content of this element, and not the element itself. Here is a working solution:

 $("link[type='text/css']").clone().appendTo($("iframe").contents().find("head")); 
+3
source share
 $("link[type='text/css']").each(function() { var stylesheet = $(this).html(); $("iframe").contents().find("head").append(stylesheet); }); 
0
source share

All Articles