0

I got this problem only on IE (even on most recent versions, like 11). The thing is that I appended an iframe dinamicly to my document in which I'm writing some scripts for later execution. Those scripts must paint an image in the end, the problem is I'm getting anoying margins on IE from the body.

I can't load that using the src, it must be dynamic. So I just thought "lets get access to the iframe and write some style on the body". But when I retrieve the iframe doc like this...

var win = (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;

... win.document.body or win.document.querySelector("body") returns null (again, in ie 11).

Does anyone know how to get to the body safely?

Vandervals
  • 5,774
  • 6
  • 48
  • 94

2 Answers2

0

You can do this way:

<iframe srcdoc="" id="iframe"></iframe>
<script type="text/javascript">
   var iframe = document.getElementById("iframe");
   iframe.setAttribute("srcdoc", "<style>body{background:red;}</style>");
</script>

Demo: http://output.jsbin.com/kaquwilaka

0

I could write some css like this:

win.document.open();
win.document.write("<style>body{margin:0;}</style>");
//write other stuff
win.document.close();

This works as doesn't avoid any further writting and also because style can be in the head or in the body.

Vandervals
  • 5,774
  • 6
  • 48
  • 94