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.

TooTallNate
  • 191
  • 4
  • 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. – Marcel Korpel Jun 10 '10 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! – TooTallNate Jun 11 '10 at 02: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. – Marcel Korpel Jun 11 '10 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... – John Jul 13 '10 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. – BoffinBrain Jun 03 '11 at 14:13
  • It's worth pointing out that even if you do get this working, it's possible to load Javascript code into a page using Ajax techniques. Code loaded in this way may not have any link back to the source file where they came from. Also, code may be run via `eval()` or similar, which would have the same effect, even if the code did originally come from a regular ` – Spudley Jul 03 '11 at 15:22
  • How could some one get script-file-name when ie(6-7) itself can't handle it without any exception...when an error occurs in any file, any where ie itself is pointing to page with a line number that i don't understand how is computed, maybe: error-line-number + 10 / 2 + log(script-length) – Beygi Jul 03 '11 at 22:10

2 Answers2

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.

Schroedingers Cat
  • 3,099
  • 1
  • 15
  • 33
1

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!

jabley
  • 2,212
  • 2
  • 19
  • 23