Reputation: 149
I want the javascript loaded only on the domain that I specify, while if it is loaded by another domain, the page will be directed to the domain that I specify.
for example, I have a page in domain mydomain.com
with some javascript there, then someone copy that javascript from my page and load it in hisdomain.com
. Because I have set the script to function only in the domain mydomain.com
, so the script will not work in hisdomain.com
. Besides, it would be better if hisdomain.com
redirected to mydomain.com
.
I want to use this method to avoid theft and cloning scripts by people who are not responsible.
Thank you!
Upvotes: 1
Views: 4603
Reputation: 1
this will help
if (location.host == "yourwebsite.com") {
// put your script function here
}
else {
location.href = "http://yourwebsite.com";
}
Upvotes: 0
Reputation: 664936
I want the javascript loaded only on the domain that I specify, while if it is loaded by another domain, the page will be directed to the domain that I specify.
This should do it:
if (location.host != "your.domain") location.href = "http://your.domain";
I want to use this method to avoid theft and cloning scripts by people who are not responsible.
That line will only help against the case that the other domain includes your.domain/script.js
(and steals your bandwidth). When the script is actually copied (and not served from your server), it would be trivial to remove such a line. It's impossible to protect. Rather have a look at How can I obfuscate (protect) JavaScript?
Upvotes: 4