5

Is there a way to find the origin of the current running script? I would like to add a different behavior depending where the script was loaded from.

e.g. was loaded from:

http://localhost:8080/js/myscript.js 

vs

http://www.myhost.com/js/myscript.js

I'm not the one who loads is so I can't add some info in load time, and the script is loaded dynamically using $.getScript(), so I can't look for the element.

Liam
  • 27,717
  • 28
  • 128
  • 190
Guy Korland
  • 9,139
  • 14
  • 59
  • 106
  • since your calling `$.getScript()` you already have that information. just assign the `url` to a parameter rather than entering the `url` directly then just check that parameter to find out where the script came from. – Jake Aitchison Aug 15 '13 at 12:04
  • not cross-browser compatible at the moment, but interesting anyway: https://developer.mozilla.org/en-US/docs/Web/API/document.currentScript?redirectlocale=en-US&redirectslug=DOM%2Fdocument.currentScript – Stefan Neubert Aug 15 '13 at 12:11
  • see my answer, should work. – Jake Aitchison Aug 15 '13 at 12:32
  • @StefanNeubert that is exactly the API I was looking for. – Guy Korland Aug 15 '13 at 13:37

3 Answers3

6

When a script is called, unless it is marked as defer or async, it will always be the last element on the page at that moment in time (since it's blocking).

Using this, you can do the following:

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

Then do with that whatever you want.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
2

Well .., its kind of a hack..!

First you need to get all script elements

var all_scripts = document.getElementsByTagName('script');

choose the current script

var current_script = all_scripts[all_scripts.length-1];

Now you can see the src of the script

alert(current_script.src);
JoSSte
  • 2,953
  • 6
  • 34
  • 54
Amith
  • 1,424
  • 1
  • 10
  • 22
-3

UPDATED changed removed var o = $(this) as per @FelixKling suggestion

UPDATED EXAMPLE

$.getScript("http://localhost:8080/js/myscript.js")
    .done(function(data, textStatus, jqxhr) {

        // this.url is the script url passed to $.getScript
        if(this.url.search('http://localhost:8080') != -1){
            alert("do one thing for http://localhost:8080/js/myscript.js");
        }
        if(this.url.search('http://www.myhost.com') != -1){
            alert("do another thing for http://www.myhost.com/js/myscript.js");
        }
    })
    .fail(function(){
        alert("somethign went wrong");
    });

Example on jsFiddle

Jake Aitchison
  • 1,079
  • 6
  • 20
  • You can avoid typing `o[0]` if you simply don't pass `this` to jQuery: `var o = this;` or just use `this.url` everywhere. `this` refers to the `jqXHR`, which is neither a DOM element nor an HTML string, so there is no reason to pass it to jQuery (`$`). – Felix Kling Aug 15 '13 at 13:01
  • @milkshake I'm not sure how is that related to problem... – Guy Korland Aug 15 '13 at 13:39
  • @GuyKorland because you can check directly which URL the `$getScript()` was using and do different behaviours based on that, just like your original question. – Jake Aitchison Aug 15 '13 at 13:40
  • @milkshake But my code is in "http://jsfiddle.net/js/Actions.js" not in the loading function – Guy Korland Aug 15 '13 at 13:42
  • @GuyKorland no but your question was how can I find out the origin of the script and this does exactly that, see my updated answer and see if it makes more sense. – Jake Aitchison Aug 15 '13 at 13:44
  • @milkshake sorry but I guess I was miss understood, I'm not controlling the code that loads the script, I'm developing a library/script that is used by many others and loaded by them. And I want to know in the code (the library code) from where it was loaded. – Guy Korland Aug 15 '13 at 13:48
  • @GuyKorland ah ok, well all you need to do is check `window.location.href` in your script. – Jake Aitchison Aug 15 '13 at 13:50
  • @GuyKorland have you checked the new answer? – Jake Aitchison Aug 15 '13 at 14:12
  • @milkshake as you probably know location.href and the scripts origin has nothing todo with each other. one can load scripts from anywhere on the web. – Guy Korland Aug 15 '13 at 14:17
  • @GuyKorland so you are trying to find out if an external script (external to a 3rd party) is calling your script? im really confused with what you actually want to find out. – Jake Aitchison Aug 15 '13 at 14:18
  • he wants to find the url of the loaded script, not the loading script. – Stefan Neubert Aug 15 '13 at 15:01
  • my first answer did that but @GuyKorland said he didnt want that, he said he wanted to know who was running his script. – Jake Aitchison Aug 15 '13 at 15:05
  • current answer (un-strikethough'ed) is has no relation to the question asked. – kumarharsh Aug 15 '13 at 15:24