views:

66

answers:

1

What's the best way to insert an element into a page's HEAD directly before the closing tag using JavaScript?

...<HEAD>
<script>...</script>
<script>...</script>
<script>...</script>
<link .../>
<link .../>
<link title="MY NEW LINK ELEMENT" />
</HEAD>...

I know about the ol' insertBefore(sp1, sp2.nextSibling) trick, but that just lets you drop something after a closing tag.

+5  A: 

If you're doing straight JavaScript, it's called appendChild. jQuery has a wrapper called append. Prototype has insert which appends when not given a location.

Jim Puls
Oh... um... DUH!... jeeze. I need to take a break I think. Out of curiosity, how would you insert directly after the open tag then?
MrSparky
@MrSparky parentElement.insertBefore(newElement, parentElement.firstChild)
insin