Let's say we have this markup:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>project.js</title>
<script src="project.js"></script>
<script>
</script>
</head>
<body>
<h1>some project — javascript & html tests</h1>
<hr />
<p>
testing 123
</p>
</body>
</html>
I know that there are .prependChild()
, .appendChild()
, .innerHTML
, etc, properties and methods, but what I am looking for is how to add (append) contents after the </body>
tag closure?
I need this, without using jQuery — is it possible?
Let's say we have this markup:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>project.js</title>
<script src="project.js"></script>
<script>
</script>
</head>
<body>
<h1>some project — javascript & html tests</h1>
<hr />
<p>
testing 123
</p>
</body>
</html>
I know that there are .prependChild()
, .appendChild()
, .innerHTML
, etc, properties and methods, but what I am looking for is how to add (append) contents after the </body>
tag closure?
I need this, without using jQuery — is it possible?
Share Improve this question asked Mar 8, 2013 at 8:45 user1386320user1386320 5-
I think most browser would not let you create an invalid DOM through DOM functions. (
html
can only contain 1head
and 1body
element and ment nodes. Nothing else.) – Roman Commented Mar 8, 2013 at 8:56 -
so meaning.. if I want to add something
after body
I should just then append the parent:html
tag? – user1386320 Commented Mar 8, 2013 at 8:57 - @ZlatanO. What do you want to add?! – Matías Fidemraizer Commented Mar 8, 2013 at 8:58
-
no.. that means if you try to add anything other than ments after the body tag, the browser will either ignore it or append it to
body
. – Roman Commented Mar 8, 2013 at 8:59 -
@MatíasFidemraizer - sorry matias, for not answering your question.. I've seen now that we can add scripts:
appended or prepended to **head**
orappended or prepended to **body**
.. I'm making a script loader – user1386320 Commented Mar 8, 2013 at 9:02
2 Answers
Reset to default 8If you use 'id'.you can use these property:
document.getElementById('id').nextSibling;
document.getElementById('id').previousSibling;
It won't work in some browser because content after body
is not "legal". Anyway, this would be:
document.body.parentNode.appendChild(document.createTextNode('text after body'));
document.body.parentNode.appendChild(document.createComment('ment after body'));
http://jsfiddle/yVKk6/ and inspect the Result frame.