get javascript servername

rares_dumitrescu
rares_dumitrescu used Ask the Experts™
on
So i have a page, www.domain.com/test.html with this code:

<script type="text/javascript" src="http://www.anotherdomain.com/test.js"></script>

How can i check on http://www.anotherdomain.com/test.js from where the script has been loaded.

I mean on test.js to alert that the script has been loaded from www.anotherdomain.com
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
Hello rares_dumitrescu,

If you want to protect your script, do that on the web server side (Apache with .htaccess)
http://www.hotscripts.com/blog/prevent-hotlinking-with-htaccess/

Regards

Author

Commented:
No, i dont want to protect it.

I have a domain alias like www.doman1.com www.domain2.com www.domain3.com

And randomly shows on domain.com
 <script type="text/javascript" src="http://www.domain1.com/test.js"></script>
or
 <script type="text/javascript" src="http://www.domain2.com/test.js"></script>
or
 <script type="text/javascript" src="http://www.domain3.com/test.js"></script>

As i said, domain aliases, so test.js is 1 file. so thats why i need to know from what alias the script has been loaded.
Expert of the Year 2008
Top Expert 2008

Commented:
alert( location.hostname )
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
it shows www.domain.com, not on of the aliases from where the script loaded.
Expert of the Year 2008
Top Expert 2008

Commented:
>>not on of the aliases from where the script loaded.
OK, I see what you mean. What you need to do is give the script tag an id:

<script id="theScript" type="text/javascript" src="http://www.domain2.com/test.js"></script>

then upon load you can do:
alert( document.getElementById("theScript").src );

that should give you the full url.

Open in new window

Author

Commented:
Yeah, i tought at that, the problem is that i dont have access to the html, only to the test.js file from the aliases, so i can't put the id.

One solution is to get all element with tag name script and check for src if matches my domain list, but this si a slow solution.

i need a faster sollution...
Expert of the Year 2008
Top Expert 2008
Commented:
If you can't modify the html, then you will have to use the "slow solution". Javascript doesn't have any feature to detect things such as it's own filename.

One way you can "speed" things up is if you know know the script tag position in the document. For example, if it is the first script tag in the document then:
document.getElementsByTagName("script")[0] will give you just the first script tag. Meaning you will not have to iterate through a list of script element references.

Author

Commented:
this is available only for first script, how about if the script is on the middle of the page ? and exists other scripts before him?
Expert of the Year 2008
Top Expert 2008

Commented:
>>this is available only for first script, how about if the script is on the middle of the page ? and exists other scripts before him?
Like I said, you will have to resort to the slow way.
==>One solution is to get all element with tag name script and check for src if matches my domain list.

Other than that, since you have control over the js files, why don't you just declare a global variable in each of them.
window.sourceFile='domain1.js';

so when the js loads alert( sourceFile ) will tell you the loaded file.

IF you have a single server that "responds" to three different names "domain1.com", "domain2.com", domain3.com", then instead of linking to the js file you can link to some dynamic script. For example, if your server supports php:


<?php
//file.php
header("Content-Type: application/javascript");
echo "var sourceFile='domain1'";
echo file_get_contents('file.js');
?>

so instead of:
<script src=file.js></script>

you will now do:
<script src=file.php></script>

The part I don't understand is how you are switching among the aliases, but basically whatever method you are using, you would "record" the "chosen" alias in this line:
echo "var sourceFile='domain1.com'";

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial