How to Get the Current Script’s Parent in Javascript

The following is a method of getting the current script’s parent, which may be useful if, for example, you need to append HTML in the same location as the script:

var scripts = document.getElementsByTagName('script');
parent = scripts[scripts.length - 1].parentNode;

The above code maintains the assumption that the currently executing script is the last script in the scripts array (e.g. document.getElementsByTagName('script')), which should always be the case. This method was used in my WpDisqusPt and WpDisqusRc projects.