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.
As the "src" attribute holds the full path to the script file you can add a substring call to get the file name only.
What will happen if the jQuery script isn't there? Are you just going to output a message? I guess it is slightly better for debugging if something goes wrong, but it's not very helpful for users.
I'd say just design your pages such that this occurrence will not happen, and in the rare event it does, just let the script fail.
If you did't want use jQuery:
You can return a list of script elements in the page:
And then evaluate each one and retrieve its location:
I had issues with the above code while extracting the script name when the calling code is included inside a .html file. Hence I developed this solution:
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?