Skip to content Skip to sidebar Skip to footer

Detect Location Of Script Not The Page Where It Is Called From

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 g

Solution 1:

Short answer: No.

Long answer: You could loop over all the elements in the page, calculate an absolute URI for each, then make a JSON-P request to a server side script on a server you control that will download each script and compare it to a master copy — but that would break if the script was changed in any way. A more robost solution might be achievable with a smarter comparison algorithum, but you'll never get 100% reliability (since there might be two copies of the same script included which you wouldn't be able to distinguish).


Solution 2:

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.


Solution 3:

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); 
}

Post a Comment for "Detect Location Of Script Not The Page Where It Is Called From"