views:

539

answers:

3

I'm trying to do something like a c #include "filename.c", or php include(dirname(FILE)."filename.php") but in javascript. I know I can do this if I can get the URL a js file was loaded from (e.g. the URL given in the src attribute of the tag). Is there any way for the javascript to know that?

Alternatively, is there any good way to load javascript dynamically from the same domain (without knowing the domain specifically)? For example, lets say we have two identical servers (QA and production) but they clearly have different URL domains. Is there a way to do something like 'include("myLib.js");' where myLib.js will load from the domain of the file loading it?

Sorry if thats worded a little confusingly.

A: 

I may be misunderstanding your question but it seems you should just be able to use a relative path as long as the production and development servers use the same path structure.

<script language="javascript" src="js/myLib.js" />
johnmdonahue
The problem is that this javascript library is meant to be loaded on different domains.
B T
So is this a single js library file located on one domain that needs to be pulled in across multiple domains? Or is this a js file that resides on multiple domains that need to pull in other js files relative to its source? Could you provide an example of what you are trying to accomplish?
johnmdonahue
Its both actually. The js file will reside on multiple domains (QA vs production), and it will also be included from multiple domains (whatever web app needs to use the library).
B T
This may be what you're trying to get around, but could you hardcode the path of the script as a variable and have devel and production versions of your lib that have that line being their only difference? Or more simply pass something to a single script as a get param that specifies its location: ?devel=true
johnmdonahue
That is what I'm trying to avoid. I'm also trying to avoid a GET parameter, but thats a perfectly valid solution. I'm just too lazy to much around in all our Java Struts stuff for this. : )
B T
Ha. Fair enough. ;)
johnmdonahue
A: 

Regardless of whether its a script, a html file (for a frame, for example), css file, image, whatever, if you dont specify a server/domain the path of the html doc will be the default, so you could do, for example,

<script type=text/javascript src='/dir/jsfile.js'></script>

or

<script type=text/javascript src='../../scripts/jsfile.js'></script>

If you don't provide the server/domain, the path will be relative to either the path of the page or script of the main document's path

Graza
+6  A: 

Within the script:

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

This works because the browser loads and executes scripts in order, so while your script is executing, the document it was included in is sure to have your script element as the last one on the page. This code of course must be 'global' to the script, so save 'src' somewhere where you can use it later. Avoid leaking global variables by wrapping it in:

(function() { ... })();
InfinitiesLoop
This is a great suggestion, as long as the code in the script is run "inline" (i.e. in a global context or "immediate run anonymous method", as you've suggested, and not in a "called later" function or set-timeout). You could create a var in each javascript for the current file/path in this way. Careful however, "leaking global variables" cannot be solved by what I've called immediate-run-anon-methods, `(function(){...})();` - your example above will still create a global var called `src` because it does not have a `var` specifier before it
Graza
That is pretty brilliant. One problem is that if I use jquery's getScript method, it fails (returns blank). Any solution to that?
B T
There is a var specifier for 'src' (see comma on end of the first line)
InfinitiesLoop
@B T: don't use jQuery. :) Ooops, just kidding!
Marco Demajo
@InifinitesLoop: in a similar answer to a similar question they suggest to return scripts[scripts.length-1].getAttribute('src', -1), see more here: http://stackoverflow.com/questions/984510/what-is-my-script-src-url/984656#984656
Marco Demajo
The issue of whether the url is resolved or not doesn't usually matter, since you'd probably want your code to work no matter how the 'src' attribute is specified. And whatever the value is, if you're using it in the same page context it came from, it will do the right thing.
InfinitiesLoop
@B T: beaware that InifinitesLoop code might return script src as a relative path on IE 6/7 (i.e. '/path/script.js') and the same script src as a full http path on IE8 / FF / Chrome / Safari (i.e. 'http://domain.com/path/script.js'). The code here is more cross-browser: http://stackoverflow.com/questions/984510/what-is-my-script-src-url/3865126#3865126
Marco Demajo