Javascript域保护脚本

Javascript域保护脚本,javascript,javascript-events,Javascript,Javascript Events,我已经制作了一个.js文件,我想保护它一点。很显然,更先进的人会从小骗子那里弄明白这一点 我想在我的脚本中添加一些东西来检查文件在哪个域上运行。如果它不是我的两个域(example1.com,example2.com),它将重定向到我的域。有点像iframe buster 然后我可以把整个代码弄糊涂一点。我的目的是防止有人下载.js并在他们的服务器上使用它 我的代码: if(typeof(width)=='undefined')width=655; if(typeof(height)=='und

我已经制作了一个
.js
文件,我想保护它一点。很显然,更先进的人会从小骗子那里弄明白这一点

我想在我的脚本中添加一些东西来检查文件在哪个域上运行。如果它不是我的两个域(
example1.com
example2.com
),它将重定向到我的域。有点像iframe buster

然后我可以把整个代码弄糊涂一点。我的目的是防止有人下载.js并在他们的服务器上使用它

我的代码:

if(typeof(width)=='undefined')width=655;
if(typeof(height)=='undefined')height=540;
if(width<655)width=655;if(height<540)height=540;
if(width<height)height=width;
document.write('<ifr'+'ame src="/streams/hd/'+ ch+'.php" scrolling=no frameborder=0 width='+ width+' height='+ height+' scrolling=no allowtransparency=true id=thatframe ></ifr'+'ame>');
if(typeof(width)='undefined')width=655;
如果(typeof(height)=‘未定义’)height=540;

如果(width使用返回主机名的
document.location.host
,然后像

var _host = document.location.host;
if( _host == "example1.com" || _host == "example2.com" ){
    // do things for example1.com
}

如果你想保护你的JS文件,那么就使用压缩版本。

非常基本的解决方案,我知道有更好的方法获取当前URL

if (document.URL.indexOf('http://example.com') == 0) {
   // Then you're on example.com.
}

请注意,这也适用于子文件夹,例如..

您也可以像这样使用document.domain(


这对我很有帮助。非常感谢。保护JS文件的最好方法是使用JavaScript浏览器。
var allowedDomains = ['example1.com', 'example2.com'];

if (allowedDomains.indexOf(document.domain) == -1) {
  // Code to redirect here
}