How might I get the script filename from within that script?

html, javascript

Solution

var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length-1];
var scriptName = lastScript.src;
alert("loading: " + scriptName);

Tested in: FF 3.0.8, Chrome 1.0.154.53, IE6

See also: How may I reference the script tag that loaded the currently-executing script?

Problem

I'm pretty sure the answer is no, but thought I'd ask anyway. If my site references a scripted named "whatever.js", is it possible to get "whatever.js" from within that script? Like: ``` var scriptName = ??? if (typeof jQuery !== "function") { throw new Error( "jQuery's script needs to be loaded before " + scriptName + ". Check the <script> tag order."); } ``` Probably more trouble than it's worth for dependency checking, but what the hell.

Original source

Related problems