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.)
To retrieve just the URL as a string, the read-only document. URL property can be used. Sun reference: Do not use location as a property of the document object; use the document. URL property instead.
open('GET', 'http://www.mozilla.org', true); request. onreadystatechange = function(){ if (request. readyState === 4){ if (request. status === 404) { alert("Oh no, it does not exist!"); } } }; request.
JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With