I'm serving a javascript file from my server. How do I prevent direct linking to it so that the server doesn't get used to serve the javascript to other websites?
3 Answers
The usual techniques for blocking hotlinking apply.
-
Yes, but there are some Javascript techniques as well, like Martin's answer.– FlimmAug 27, 2015 at 12:27
To discourage hotlinking you could put annoying code in the script and have that execute if window.location
doesn't match your own site:
while (window.location.hostname !== 'www.example.com')
alert('Plz stop hotlinking');
-
-
1@sami yes, but you would render the script unusable to others and nobody in their right mind would link to it.– MartinMar 25, 2011 at 9:23
There is no good way to do so. However, you could do some referer checks or check the values from the location
object and make the JavaScript do bad things when embedded from a different site.
When doing referer checks don't forget to allow an empty referer as some people block referers.