views:

75

answers:

3

I'm trying to find the url for the currently executing javascript. I know I can use window.location.href for the current page, but that's not necessarily the path to the script which is executing.

Any help is greatly appreciated.

Thanks.

EDIT 1: I'm fully open to the use of plugins and such to accomplish this. Don't hesitate to suggest anything.

EDIT 2: I need this so that I can find a relative path from where my currently executing script is at to another resource. The issue is that, for example, the script is running at /js/myScript.js and the resource I need to reference is at /imgs/test.png. Since the .js file can be included from anywhere, the path to the .png file cannot be known.

A: 

I don't think you can do this with JavaScript. You'd need a JavaScript trace plug-in.

EDIT: Firebug can help you with this, as can the IE8 debugger (press F-12) .

Diodeus
I'm not familiar with trace plug-ins. Any suggestions? I'm guessing a trace plugin would slow performance?
Brian Hasden
Sure it is possible
streetparade
+1  A: 

Try this but i cant guarantee that it will work with all browsers in my firefox it worked:

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

var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/);
alert(scriptName);

Will alert the script name:

streetparade
How does that work if there's more than one script included on the page? It seems to me as though it'd just grab the last script tag and display it's name.
Brian Hasden
It works if you are in the main body of the script, as it's the last one in the document (the rest haven't been parsed yet). You then put the `src` in a variable and access it when you need to at a later stage. (Forget the `scriptName` bit.)
bobince
My scripts are more well behaved than just running everything when they're included. Using the jQuery framework most of my scripts don't actually do anything special until the document ready event. Sorry for the confusion. Considering there are a half dozen scripts included I'd hate to run that same getElementsByTagName at the beginning of every script include. It would slow down page rendering which isn't really acceptable in this scenario. I'd much rather just set a global root path variable which is what I'm trying to get around doing.
Brian Hasden
Sure, you can grab the script's name, but that has nothing to do with whether it is "executing". What if there are multiple scripts?
Diodeus
@Brian: You run the last-script-src sniff when you're included, then just store it in a script-specific variable and do nothing else. This really isn't slow at all; it will be way faster than anything jQuery is doing. You can then use the variable later when you're actually doing stuff. But yes: a global root path variable is the normal approach, since you often need that for other purposes too. And a global root is necessary where this approach won't work: for `defer` and HTML5 `async` scripts (which are currently rare).
bobince
Thanks bobince, looks like Resig was actually toying with this type of code for a while. Not sure what came of it, but he was using it to run code inside of `<script src="">` tags. Pretty interested actually. You can check it out at http://ejohn.org/blog/degrading-script-tags/. Upvoted for the help and education.I think I'm just going to go with the base tag for now and see how that treats me.
Brian Hasden
I cant see any up vote. Did you realy upvoted?
streetparade
I had upvoted bobince's comments and forgot to get you as well. You should be good now. Thanks again.
Brian Hasden
+2  A: 

Based on a similar question at http://stackoverflow.com/questions/893144/-equivalent-in-javascript, the answer by Kamarey solved my problem.

From the answer by Kamarey:

Use base tag:

<head> 
   <base href="http://www.example.com/myapp/" /> 
</head> 

...

from now any link use on this page, no matter in javascript or html, will be relative to the base tag, which is "http://www.example.com/myapp/".

Obviously this isn't exactly what I was looking for but it solved the underlying problem I was having. By specifying the base, all requests are made relative to the base specification.

EDIT: For anyone interested, it appears as though the jQuery method getScript() does not respect the base location in Firefox 3.5.7. Obviously this may not be an actual bug in jQuery and could be more of a Firefox issue but it's worth noting. In IE8 everything seems to work appropriately. I've filed a ticket with jQuery to see if maybe they can streamline the behavior. You can track it at http://dev.jquery.com/ticket/6034.

Brian Hasden