Jump to content

DOM question


Kieran Menor

Recommended Posts

If I do something like this, it is possible to place some HTML right at the spot where you place the <script> tag:

<script type="text/javascript>
var p = "<p>This is a paragraph.</p>";
document.write(p);
</script>

 

My question is if it is possible to do something similar for a DOM object:

<script type="text/javascript>
var p = document.createElement("p");
p.appendChild(document.createTextNode("This is a paragraph."));
</script>

 

So if I want p to be placed right at where the <script> tag is, what do I do? Or even, is it possible to get the object of the <script> tag's parent node?

Link to comment
Share on other sites

my understanding is that, if the body tag has already been parsed by the browser, then document.write would be the essentially the same as using

 

document.body.appendChild()

 

So, if you used this in between a tr and a td tag (i.e., not inside the table cell, but sorta in table oblivion), the paragraph would be placed above the table.

 

Hope that helps...

Link to comment
Share on other sites

you need to use the dom to to tell it where to insert the new node you created...

 

so for your script...

 

<script type="text/javascript" id="addP">
var p = document.createElement("p");
p.appendChild(document.createTextNode("This is a paragraph."));
document.getElementById('addP').insertbefore(p);
</script>

 

there is no native insertafter method in javascript or at least as far as I am aware but as this is a script tag it doesn't really matter....

Link to comment
Share on other sites

Not really. :(

 

document.body.appendChild() would just append the node to the body element, even if the <script> tag was placed in, say, a <div> tag. Thus, it would not have the intended effect.

 

That was my point. Thus your original question and first script example wouldn't do what you want.

 

Even if you appended the element to the script element, it's EXACTLY THE SAME as calling document.write at that point.

 

Hope that makes sense. You're appending to what's there in the body at the point the script is run.

 

if you have

 

 

<body>

 

<div>

 

<script>

.... code here

</script>

 

 

document.write only sees what's on the page, there is no </div> tag yet, and neither would document.body.apendChild

 

So, inserting after the script tag is no different than either of those two.

 

Hope that makes it clearer...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.