1

I insert an external .js file from another domain. Like this:

<script src="http://externaldomain.com/file.js" type="text/javascript"></script>

file.js is always the same, but it can be placed in different domains,

so I need to know what is the source of the file.

For example: file.js is in domain1.com, domain2.com and domain3.com.

If someone insert it like this: <script src="http://domain1.com/file.js" type="text/javascript"></script>

I want to know that the file is inserted from domain1.com

How to do it?

Jonathan
  • 8,676
  • 20
  • 71
  • 101
  • This is a duplicate, but I can't find the original. The bottom line is that it is possible only using workarounds. There is no native function to do this – Pekka Nov 21 '10 at 09:46
  • workarounds like adding an 'id' to ' – Jonathan Nov 21 '10 at 09:49
  • Yes, that would work. That's pretty much what Darin's suggesting in his answer, although I think he assumed you couldn't rely on being able to change the `script` tag to add a unique identifier to it. If you can, that'll make it a lot more reliable. – T.J. Crowder Nov 21 '10 at 09:51
  • 2
    @Pekka: Indeed: http://stackoverflow.com/questions/3548857/how-to-get-the-location-src-of-a-javascript-file http://stackoverflow.com/questions/710957/how-might-i-get-the-script-filename-from-within-that-script http://stackoverflow.com/questions/984510/what-is-my-script-src-url http://stackoverflow.com/questions/970444/detect-location-of-script-not-the-page-where-it-is-called-from http://stackoverflow.com/questions/3206050/extract-src-attribute-from-script-tag-and-parse-according-to-particular-matches but I know there's another, better one, with an example on GitHub. – Marcel Korpel Nov 21 '10 at 11:31

1 Answers1

4

There's no reliable way of achieving this. Basically inside this file you will need to parse the DOM, search for all the <script> tags and when you find the one that corresponds to the inclusion of this javascript file parse the src attribute to extract the domain.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928