19

Hey all, I've been trying to throw together a generic function that retrieves the absolute URL of an executing JavaScript file on a web page:

http://gist.github.com/433486

Basically you get to call something like this:

getScriptName(function(url) {
    console.log(url);
    // http://www.example.com/myExternalJsFile.js
});

inside an external JavaScript file on a page and can then do something with it (like find the <script> tag that loaded it for example).

It works great in almost all the browsers I've tested (Firefox, Chrome, Safari, Opera v10 at least, and IE 8).

It seems to fail, however, in IE 6 and 7. The callback function gets executed, but the retrieved name is the URL to the main HTML page, not the JavaScript file. Continuing with the example, getScriptName invokes the callback with the parameter: http://www.example.com/index.html

So all I'm really asking is if there's some other way of getting the URL of the current JavaScript file (which could be IE 6 and 7 specific hackery)? Thanks in advance!

EDIT: Also, this won't work in every case, so please don't recommend it:

var scripts = document.getElementsByTagName("script");
return scripts[scripts.length-1].src;

I'd like it to work in the case of dynamically created script tags (possibly not placed last in the page), aka lazy-loading.

7
  • 1
    Nice code and question. Although I don't know an answer to this, I just want to give you a few optimizations: you can change every obj['prop'] to obj.prop and as a typeof always returns a string, you don't have to test for identity (===); testing for equality (==) is good enough. These perform slightly better than the things in your code. Commented Jun 10, 2010 at 23:46
  • Thanks. Ya, I know I can replace obj['prop'] to obj.prop, I just had it that way in preparation of having the function sent to Google Closure Compiler, which would mangle those variable names. I'll change the === to a == though, thanks! Commented Jun 11, 2010 at 2:29
  • I just tested accessing obj['prop'] against obj.prop, but contrary to what I thought was the case, in Firefox the difference was negligible, whereas in Chrome the former was actually faster than the latter. I'm really surprised about this, it might have something to do with the 'compilation' of JavaScript by V8. Commented Jun 11, 2010 at 23:47
  • It may be my shortcoming, but what is the point? If you knew where to embed the script into your html you know where you are calling it from... Commented Jul 13, 2010 at 22:37
  • 4
    I agree with John. Let's take a look at the bigger picture and work out why you want the scripts' file names. That sounds like brittle programming to me. Commented Jun 3, 2011 at 14:13

2 Answers 2

1

A lot of this depends on what you have access to. If, as it appears, you are trying to do this entirely within the JS code, I do not believe that you are able to do it, for some of the reasons shown above. You could get 90% of the way maybe, but not be definitive.

If you are working in a dotnet environment ( which is the only one I know ), I would suggest the use of a module that would intercept all JS requests and add into them the request location, or something of that nature.

I think you need to address this from the server side, not the client side. I do not think you will have a definitive answer form the client side. I think you will also struggle to get an answer from the server side, but you might be more successfull.

Sign up to request clarification or add additional context in comments.

Comments

1
+50

Sorry, I suspect you might struggle with this. IE earlier than version 8 typically gives error messages from javascript errors of the form:

line: 342
char: 3
error: expected identifier, string or number
code: 0
url: http://example.com/path/to/resource

where the url is the window.location.href, rather than the URL of the external Javascript resource that contains the problem. I suggest that IE gives the unhelpful URL value since the script URL isn't available to IE at that point, and neither is it available to any Javascript you might write to try to display it.

I would love to be able to link to IE8 release notes which say this bug / feature has been fixed, hence the reason I created this as community wiki. My MSDN foo is pretty weak!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.