Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 343
core Avatar asked Apr 02 '09 18:04

core


People also ask

How do you get the script name inside a script?

If you invoke the script with path like /path/to/script.sh then $0 also will give the filename with path. In that case need to use $(basename $0) to get only script file name.

What is the best way to get the filename of the current running script?

Just use the PHP magic constant __FILE__ to get the current filename.


1 Answers

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?

like image 172
Shog9 Avatar answered Sep 16 '22 16:09

Shog9