Kieran Menor Posted April 3, 2009 Share Posted April 3, 2009 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? Quote Link to comment Share on other sites More sharing options...
Floydian Posted April 4, 2009 Share Posted April 4, 2009 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... Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted April 5, 2009 Author Share Posted April 5, 2009 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. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 6, 2009 Share Posted April 6, 2009 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.... Quote Link to comment Share on other sites More sharing options...
Floydian Posted April 6, 2009 Share Posted April 6, 2009 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... Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted April 8, 2009 Author Share Posted April 8, 2009 No. You are completely wrong. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 10, 2009 Share Posted April 10, 2009 document.write is not the way to do this......... you have the DOM so use it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.