views:

361

answers:

1

How can I insert all of a parent window's stylesheets into an iframe's head(samedomain)?

My attempted 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();
}

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

Edit: Attempt based on Anthony's answer:

 $("link[type='text/css']").each(function() {
  var stylesheet = $(this).clone();          
  $("iframe").contents().find("head").append(stylesheet);
 });
A: 
$("link[type='text/css']).each(function() {
     var stylesheet = $(this).html();
     $("iframe").contents().find("head").append(stylesheet);
     });
Anthony
you mean this.clone right? Because otherwise that would move the stylesheet. Still, I'm not getting this to work.
Mark
No, i meant append, but I also meant to specify to append the html, sorry. Does the edit work? If not, make sure the iframe selector works. Something simple like `alert($("iframe").contents().find("title").text()`. The iframe code is straight from jquery.
Anthony
Sorry, I messed some little thing up, but the vers I have in the question would work. Thanks.
Mark