2

Does anyone know if there is a way to add a iframe directly under the <body> tag? I see the appendChild but that does it before </body>. Any suggestions will be helpful.

Also, I doubt there is a way. But the website that is inside the iframe. Is it possible to have there drop downs, hover beyond the restricted iframe?

   ifrm = document.createElement("IFRAME"); 
   ifrm.setAttribute("src", "http://example.com"); 
   ifrm.style.width = "100%"; 
   ifrm.style.height = "30px"; 
   document.body.appendChild(ifrm); 
Richard Dev
  • 1,110
  • 7
  • 21
  • 1
    Your term "directly under the body" is ambiguous. It could mean "as a child of the body" which your example code succeeds at, or "as the *first* child of the body" as Sep O Sep's answer assumes, or "following the body" (as `...`) as Quentin's answer appears to assume. – Stephen P Sep 21 '11 at 00:45

2 Answers2

2
document.body.insertBefore(ifrm, document.body.childNodes[0]);
Sebastian Otto
  • 15,139
  • 4
  • 18
  • 21
0

Anywhere (in HTML) that you are allowed to have a <body> element allows exactly one <body> element and nothing else. Thus, you cannot have an <iframe> as a sibling to a <body>.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • you can have any number of `body` elements side-by-side as you wish*, its against standards, but its very possible. *limited only by engine's max umber of siblings. – c69 Sep 20 '11 at 23:33
  • 1
    @c69 — HTML allows one and only one body element in any place that it allows a body element at all. Some browsers may allow a DOM that can't be converted to a valid HTML document, but that's no reason to go and do it. – Quentin Sep 20 '11 at 23:36
  • html is soft standard, and people do all kind of crazy and idiotic things with it.. so browsers just had to adapt and somehow still render this kind of very invalid markup. – c69 Sep 20 '11 at 23:41
  • 1
    So? Depending on undocumented, non-standard error recovery is *still* a very very bad idea. – Quentin Sep 20 '11 at 23:44
  • 1
    It depends on _what_ you are planning to do ;) – c69 Sep 20 '11 at 23:46
  • I am not sure my question was clear. I wasn't wanting more than one body tag. I wanted a iframe contained at the top of html page. – Richard Dev Sep 21 '11 at 21:21