Reputation: 8358
If I have a site, example.com, and a page on that site references a Javascript at subdomain.example.com/serveAd.js -- is there a way from within serveAd.js to know its own URL, or the domain from which it was downloaded?
(The JS can certainly know all about the page that called it. I want to know if the JS knows about its own origin.)
Upvotes: 21
Views: 6106
Reputation: 9110
If you were using jquery, you could get the full url with this kind of thing:
var fullJsUrl= $('script[src$=serveAd.js]').attr('src');
If not using jquery, it may take more work, but you get the idea ;)
Upvotes: 15
Reputation: 106382
I'm pretty sure, as the script is parsed that the last <script>
node available in the DOM will be the one being parsed.
Try this in an external JS to see:
var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length - 1];
alert(lastScript.src);
Upvotes: 6