ElementA <script>by default is executed immediately, so at the time of execution there is only <div>HI! <script />a part of the “How are you” part in the document that has not yet been processed. At this point, the processed <script>element will be the last of the script elements in your document, so you can reference it with document.scripts[ document.scripts.length - 1 ], then find its parent node and add the elements.
<div>
HI!
<script>
var spanNode = document.createElement('span');
var textNode = document.createTextNode('there.');
spanNode.appendChild( textNode );
document.scripts[ document.scripts.length - 1 ].parentNode.appendChild( spanNode );
</script>
How are you?
</div>
http://jsfiddle.net/0LsLq9x7/
: , , : http://jsfiddle.net/0LsLq9x7/1/
<div>
HI!
<script>
(function( scriptElement ){
var spanNode = document.createElement('span');
var textNode = document.createTextNode('there.');
spanNode.appendChild( textNode );
scriptElement.parentNode.appendChild( spanNode );
}(document.scripts[ document.scripts.length - 1 ]));
</script>
How are you?
</div>