You can locate the current script element using document.currentScript
. Note that Internet Expolorer does not support it at all (view compatibility table at https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript), so a simple way to get the current script element in a corss-browser way is to give your tag a fixed id, then locate it with document.getElementById()
.
For performace reasons, before using the DOM, try to retrive the current script tag with document.currentScript
.
As an example, having an html like this
<div id="content">
<h1>Shop Sample</h1>
<h2>View HTML-Source to see the embedded js</h2>
<script id="script-id" src="js/faq_embd.js"></script>
<div id="response-area"></div>
</div>
You could use this code to create a new paragraph just under the current script tag:
var yourScript = document.currentScript || document.getElementById("script-id");
var yourParagraph = document.createElement("p");
if (yourScript.nextSibling){
yourScript.parentNode.insertBefore(yourParagraph, parentGuest.nextSibling);
} else {
yourScript.parentNode.appendChild(yourParagraph);
}