0

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?

sami
  • 155
  • 1
  • 3
  • 6

3 Answers3

3

The usual techniques for blocking hotlinking apply.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 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

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