In the moment in which the current script is being executed, will be the last script
element in the DOM, therefore you can get it by:
var scripts = document.getElementsByTagName('script'),
currentScriptSrc = scripts[scripts.length-1].src;
Check this example that loads this script.
Edit: Taking in consideration the @kangax's comment, about the async
and defer
attributes, the only safe way IMO, previously knowing the file name, would be to inspect the script
elements on the page, examining its src
attribute, some libraries like Scriptaculous.us use this technique, for example:
var scripts = document.getElementsByTagName('script'),
len = scripts.length,
re = /howdy\.js$/,
src, howdyScriptSrc;
while (len--) {
src = scripts[len].src;
if (src && src.match(re)) {
howdyScriptSrc = src;
break;
}
}