Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Insert in place without document.write (See related posts)

// http://www.sitepoint.com/blogs/2007/07/11/insert-in-place-without-documentwrite/

// So, given a syndication script with a fixed ID:
<script type="text/javascript" id="syndication" src="syndication.js">script> 

// We can go from oldskool nastiness like this:
document.write('

Here is some syndicated content.

'); // To modern loveliness like this: var newcontent = document.createElement('p'); newcontent.id = 'syndicated-content'; newcontent.appendChild(document.createTextNode('Here is some syndicated content.')); var scr = document.getElementById('syndication'); scr.parentNode.insertBefore(newcontent, scr); // We could even go a step further and remove the <script> ID, but in that case we would need a concrete method for identifying the specific element. We could do that by knowing its SRC: var scripts = document.getElementsByTagName('script'); for(var i=0; i<scripts.length; i++) { if(scripts[i].src == 'http://www.mydomain.com/syndication.js') { //scripts[i] is the one break; } }

You need to create an account or log in to post comments to this site.


Related Posts