As mentioned in What is the current element in Javascript?, the current <script>
element is also the last <script>
element, at the time of execution.
The answer also applies to your case, since the load of the external script is not deferred (through the async
or defer
attribute).
var scriptTag = document.getElementsByTagName('script');
scriptTag = scriptTag[scriptTag.length - 1];
var parentTag = scriptTag.parentNode;
Most browsers (even IE6) support document.scripts. Firefox supports this object as of version 9. So, the following code can also be used:
var scriptTag = document.scripts[document.scripts.length - 1];
var parentTag = scriptTag.parentNode;