开发者

Javascript file for single domain

How can I make a javascript file only work on a single domain. I'll compress this file and when somebody tries to use it by copying my web site or just the javascript file, it won't work and give alert.

Here is an example. I dow开发者_StackOverflow中文版nloaded all files but when try to view it gives an alert that says I need to buy it (what exactly I'm going to do :)


That's not possible, javascript can be obfuscated but an adept developer can always figure that out and use your files. You can copyright your file but still in countries where there is no privacy policies, this won't matter.


You can't really do this. You could put something at the beginning of your code like this:

if (window.location.href.substring(0, 18) != "http://example.com") {
    alert("You thief! Stop using my code!");
}

...but all that means is that the person using it would remove that from their copy. Obfuscation might help, but not necessarily by much. Anything the Javascript interpreter can interpret, someone can copy and reuse. All you can do is set the bar a bit higher, and you probably have more important things to do.


In scripting languages the source code and the program are the same as opposed to compiled languages in which you compile the source code into a binary which is in a different format. That's why (as others said) you can't protect your JavaScript code from being used by others. The only method (other than the obfuscation that others mentioned) is to move the implementation elsewhere, you can create a web-service containing the logic you want to protect and then have your JavaScript call this using AJAX (and verify that it comes from your site).

This has the obvious disadvantages of being slower and making you supply processing power instead of utilizing the client side processors.


Run it through JSMIN (uglifier), if you are still worried, javascript is the wrong solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜