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?
Asked
Active
Viewed 855 times
3 Answers
3
The usual techniques for blocking hotlinking apply.
-
Yes, but there are some Javascript techniques as well, like [Martin's answer](http://stackoverflow.com/a/5429700/247696). – Flimm Aug 27 '15 at 12:27
3
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');

Martin
- 37,119
- 15
- 73
- 82
-
-
1@sami yes, but you would render the script unusable to others and nobody in their right mind would link to it. – Martin Mar 25 '11 at 09:23
1
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.

ThiefMaster
- 310,957
- 84
- 592
- 636