1

My code so far:

var iframe = document.createElement('iframe');
var html = '<style>.alert {padding: 20px;background-color: blue;color: white;}</style><div class="alert">test</div>';
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();

My problem is that I want to put this Alert BEFORE my current HTML structure. I know append is obviously putting it AFTER, but is there a workaround or some other method to move it so it's before?

EDIT: the implemented iFrame (generated through JS) is disappearing after 1-2 seconds: Reason??

Jamie Barker
  • 8,145
  • 3
  • 29
  • 64
Hendrik Breezy
  • 128
  • 1
  • 12
  • 2
    On top as in overlapping and covering up the other html? Or prepending: http://stackoverflow.com/questions/3391576/how-can-i-implement-prepend-and-append-with-regular-javascript – nnnnnn Sep 16 '16 at 14:47
  • Oh you want to prepend the new `div` to so that it shows up at the top of the html file? http://stackoverflow.com/questions/3391576/how-can-i-implement-prepend-and-append-with-regular-javascript – sova Sep 16 '16 at 14:47
  • You should try changing the position parameter in your style tag for `.alert`. If you do not set this parameter, the position parameter will be static by default, which means it will try to position itself next to other static elements. – Glubus Sep 16 '16 at 14:48
  • 7
    The amount of misinformation in some of these comments is outstanding. – André Dion Sep 16 '16 at 14:49
  • 1
    I'll see if thats it. (Yes I want to put it before my HTML, not overlapping or anything) FYI, it's an intranet webpage so adblockers shouldnt be a problem – Hendrik Breezy Sep 16 '16 at 14:49
  • So the iframe is on top now, but why is it disappearing after some sec? – Hendrik Breezy Sep 16 '16 at 14:56
  • I don't understand. Do you want to insert the iframe at the beginning of the body, or insert that HTML at the beginning of the iframe (which is empty, so it shouldn't matter). – Oriol Sep 16 '16 at 15:05

0 Answers0