Insert elements at <script> tag position
At script inclusion time, it's certain that the last <script>
in the page will be the current one; the rest of the page hasn't been parsed yet. So:
<script type="text/javascript">
var scripts= document.getElementsByTagName('script');
var this_script= scripts[scripts.length-1];
// Something that happens later...
//
setTimeout(function() {
var div= document.createElement('div');
div.appendChild(document.createTextNode('Hello!'));
this_script.parentNode.insertBefore(div, this_script);
}, 5000);
</script>
This holds true as long as the script tag doesn't use defer
, or HTML5's async
.