0

Here's the scenario, I own foo.js, and it's being injected into random sites (let's call it cats.com).

From foo.js, I'd like to know where foo.js is being hosted and consumed from cats.com. For example, cats.com could do something like:

<script src="www.dogs1.com/foo.js" />

or they could do:

<script src="www.dogs2.com/foo.js />

In the first example, I'd like to print dogs1.com, and in the second I'd like to print dogs2.com. Is that possible?

pfrank
  • 2,090
  • 1
  • 19
  • 26
  • 1
    You could just parse the src property of the script tags on the page (using querySelectorAll or maybe getElementsByTagName) ? – Dan H Jan 17 '14 at 20:03
  • This is not possible without a huge amount of hassle. The easiest would be to make it 2 separate resources, or have a server-side language (that will always know the host name) inject it live into the JS file, like `var hostname = " echo $_SERVER["HTTP_HOST"]; ?>"` in PHP – Pekka Jan 17 '14 at 20:03
  • The script itself does not have any reference to that info. Basically you would need to look at the script tags and match the file name and get the info out. – epascarello Jan 17 '14 at 20:03
  • possible duplicate of [Path to included Javascript page](http://stackoverflow.com/questions/1858686/path-to-included-javascript-page) – Pekka Jan 17 '14 at 20:03
  • Get the script tag, and then get its `src` attribute. http://stackoverflow.com/a/3326554 – gen_Eric Jan 17 '14 at 20:04

0 Answers0