4

IE does not allow writing to the innerHTML property of style or head elements. So how do you copy a style element from the head of one document to another?

lambacck
  • 9,768
  • 3
  • 34
  • 46

2 Answers2

1
function copy_style(src_style_tag) {    
    var tmp_div = document.createElement('div');
    var innerHTML = src_style_tag.innerHTML;
    tmp_div.innerHTML = '<p>x</p><style type="text/css">' + innerHTML + '</style>';

    return tmp_div.getElementsByTagName('style')[0];
}

The magic is that you need the <p> tag in the innerHTML of the tmp_div. Without it, IE does not accept the style element.

Alex
  • 1,457
  • 1
  • 13
  • 26
lambacck
  • 9,768
  • 3
  • 34
  • 46
0

If you want to copy some elements, than try using Node.cloneNode(true) together with Node.appendChild

Rafael
  • 18,349
  • 5
  • 58
  • 67
  • cloneNode does not work from a different document (say an iframe). You would need to use document.importNode, which is not supported by IE. – lambacck May 17 '09 at 22:17