I have an external .js file that I load on one of my sites to popular a jquery chart. I don't want someone else to be able to just copy my code and have it work for them, so is there a way to keep a .js file working only on a single domain? Kind of like how you can prevent images from being hotlinked to other domains.
-
see this: http://stackoverflow.com/questions/10224320/preventing-users-to-view-my-javascript-source-file – Kai Qing Jan 16 '14 at 00:47
-
Yes, what hosting provider are you using? Basically the technique for doing this is identical to the technique for images - you detect the referrer header the browser sends and if it doesn't match your domain you return a "FORBIDDEN" response (403 maybe?) – Benjamin Gruenbaum Jan 16 '14 at 00:53
-
Here's another question similar to yours that gives some suggestions: [StackOverflow.com/questions...](http://stackoverflow.com/questions/194397/how-can-i-obfuscate-javascript) Steven – Stevenson Prescott Jan 16 '14 at 00:53
-
my hosting provider is hostgator. – coolmusic Jan 16 '14 at 16:01
1 Answers
Depending on what your plugin does, there might be a solution. A limited way to do this is CORS - you load the script from your domain, and have it load the data from the same (sub)domain. If that server has cross-origin requests disabled, then if somebody just used your source file, their request for data would fail - because of the same-origin policy.
Of course, this does not limit the people to just copy/paste your code to their sites and work with their own code - but if you simply want to prevent your server to be a host for their javascript file, then that could do it.
Check this link for more info on Same-origin:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript
Alternatively, use server-side code to generate those graphs - as nothing can prevent clients to download your javascript source.

- 18,936
- 14
- 70
- 123