views:

840

answers:

4

Is there a simple and reliable way to determine the URL of the currently-executing JavaScript file (inside a web page)?

My only thought on this is to scan the DOM for all the script src attributes to find how the current file was referenced and then figure out the absolute URL by applying it to document.location. Anyone have other ideas, is there some super-easy method I completely overlooked?

UPDATE: Script elements accessed via the DOM already have a src property which contains the full URL. I don't know how ubiquitous/standard that is, but alternatively you can use getAttribute("src") which will return whatever raw attribute value is in the [X]HTML.

A: 

If this is a strictly client solution, yours sounds pretty good.

If you are writing code on the server, you could probably just populate a div/hidden field/(insert your fave HTML element here) with the fully resolved URL to the script, and pick that up with your javascript on the clientside.

Jason Watts
A: 

You may want to have a look at https://addons.mozilla.org/en-US/firefox/addon/10345 if you're interested in learning which functions (and thus which file) are executing on a page you don't control.

If you're interested in figuring out which of your scripts is executing, then there are a number of ways. With Firebug you could console.log() the information. Even just putting alert statements in your code (while annoying) can help debug in a low-tech way. You could also raise errors and catch them, then process using properties of the error (see: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error)

However, why would this be important? If the script is causing errors already then it's easy enough to determine where the error is occurring. If it's not about errors at all, then what's the advantage in knowing which file it comes from?

Jonathan Fingland
In my current project the JavaScript resource files are fixed to a specific sub-directory. Figuring out the script's absolute URL can tell me what the local base directory is so I can strip it off to come up with the document "key" (as known to the server-side). It'll be used for generating forms, async requests, etc. and not for debugging.
Neil C. Obremski
in that case, what you proposed above is the only solution really available.
Jonathan Fingland
+4  A: 

Put this in the js file that needs to know it's own url.

Fully Qualified (eg http://www.example.com/js/main.js):

var scriptSource = (function(scripts) {
    var scripts = document.getElementsByTagName('script'),
        script = scripts[scripts.length - 1];

    if (script.getAttribute.length !== undefined) {
        return script.src
    }

    return script.getAttribute('src', -1)
}());

Or As it appears in source (eg /js/main.js):

var scriptSource = (function() {
    var scripts = document.getElementsByTagName('script'),
        script = scripts[scripts.length - 1];

    if (script.getAttribute.length !== undefined) {
        return script.getAttribute('src')
    }

    return script.getAttribute('src', 2)
}());

See http://www.glennjones.net/Post/809/getAttributehrefbug.htm for explanation of the getAttribute parameter being used (it's an IE bug).

Crescent Fresh
Flipping GREAT! Thanks!
Marco Demajo
For the fully qualified one can't you just use script.src? Why are you using script.getAttribute('src', -1)???
Marco Demajo
A: 

After some tests here is an improved version of the one proposed by Crescent Fresh:

Fully Qualified (e.g. http://www.example.com/js/main.js):

var scriptSource = (function(scripts) 
{ 
    var scripts = document.getElementsByTagName('script'), 
        script = scripts[scripts.length - 1]; 

    return (script.getAttribute.length !== undefined) ?

           //FF/Chrome/Safari 
           script.src : //(only FYI, this would work also in IE8)

           //IE 6/7/8
           script.getAttribute('src', 4); //using 4 (and not -1) see MSDN http://msdn.microsoft.com/en-us/library/ms536429(VS.85).aspx
}());

Or as it appears in source (e.g. /js/main.js):

var scriptSource = (function() 
{ 
    var scripts = document.getElementsByTagName('script'), 
        script = scripts[scripts.length - 1]; 

    //No need to perform the same test we do for the Fully Qualified
    return script.getAttribute('src', 2); //this works in all browser even in FF/Chrome/Safari
}()); 
Marco Demajo