2

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.

2
  • It would help if you explained why you need this. Commented Feb 4, 2010 at 21:06
  • 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. Commented Feb 4, 2010 at 21:18

4 Answers 4

2

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:

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

7 Comments

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.
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.)
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.
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?
@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).
|
2

Based on a similar question at ~/ 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.

Comments

0

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) .

1 Comment

I'm not familiar with trace plug-ins. Any suggestions? I'm guessing a trace plugin would slow performance?
0

You can take advantage of errors produced by browsers and use a try/catch to find out the absolute file location.

I've coded a simple function. You can see it here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.