tags:

views:

40

answers:

3

Hello,

I am wondering if I can somehow find out the location of the script and not the page it is called from. e.g. if the page is http://xxx.yyy.com/a.htm, and I write location.href, I get that location instead of http://aaa.zzz.com/script.js where the script resides. Is it possible to get the location of the script?

Thank you for your time.

A: 

You could always grab the script tag itself and parse out the src attribute.

var scripts = document.getElementsByTagName('script');
for(var i = 0; i < scripts.length; i++){
    alert(scripts[i].src); 
}
ajm
True, but how do you know which script the code is in? I have multiple scripts on every page (well...recently I bundled them...) and you wouldn't be able to tell in which file the JavaScript was defined.
Topher Fangio
Yeah, I reached the same conclusion. David's idea of grabbing a snippet and checking that against a code repo is going to be your best shot.
ajm
+1  A: 
David Dorward
+1  A: 

I had to do this exact same thing just now and came up with a slightly different solution that's working well for me. Do you own the page that the script is being referenced in? If so, you can put an id on that script element. Then in your javascript file, you can get that element and parse it "src" attribute for the domain.

will