0

I have a javascript which outputs the domain name in console. This javascript is used in web pages hosted in different domains. The console should display the domain javascript is loaded from instead of the domain web pages are hosted. How do i get that without using jquery?

Blue Jay
  • 71
  • 1
  • 12
  • Possible duplicate of [Can code in a Javascript file know its own domain/URL](http://stackoverflow.com/questions/3097644/can-code-in-a-javascript-file-know-its-own-domain-url) – mdickin May 25 '16 at 17:57
  • Found answer in [how-might-i-get-the-script-filename-from-within-that-script](http://stackoverflow.com/questions/710957/how-might-i-get-the-script-filename-from-within-that-script) – Blue Jay Jun 02 '16 at 15:08
  • Possible duplicate of [How might I get the script filename from within that script?](https://stackoverflow.com/questions/710957/how-might-i-get-the-script-filename-from-within-that-script) – Blue Jay Sep 25 '18 at 19:40

1 Answers1

0

You will have to extract that from the URL of the script.

Example:

var url = "http://google.com/script.js";
var match = url.match(/\/\/(.*)\//);
if (match) {
    console.log(match[1]);  // This is the domain name
}

Javascript will lose the context of the original script location once it is fetched.

kvn
  • 2,210
  • 5
  • 20
  • 47