Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个JavaScript到底做什么?_Javascript_Html - Fatal编程技术网

这个JavaScript到底做什么?

这个JavaScript到底做什么?,javascript,html,Javascript,Html,这是在我的网站上找到的一个脚本 var domains = ["abc.net", "abc.com", "abc.info", "abc.blogspot.com"]; if (domains.indexOf(document.location.hostname) == -1 && document.cookie.indexOf('opened=true') == -1) { window.top.location = "http://abc.net"; doc

这是在我的网站上找到的一个脚本

var domains = ["abc.net", "abc.com", "abc.info", "abc.blogspot.com"];
if (domains.indexOf(document.location.hostname) == -1 && document.cookie.indexOf('opened=true') == -1) {
    window.top.location = "http://abc.net";
    document.cookie = "opened=true";
}

我不知道它做什么,但它总是重定向到abc.net。有人能给我解释一下它的工作原理吗?

它检查文档的域名是否不在列表中,检查文档是否包含open=true,如果两者都是true,它会将顶级文档重定向到,并将文档cookie设置为open=true。如果我没弄错的话,此代码用于跳出其他网站可能使用此脚本打开的网站的任何框架。

我认为它会检查当前页面的url是否在该域数组中,如果不在该域数组中,则将其重定向到abc.net
window.top.location
易受同源策略影响,因此无法跳出其他网站的iframe(例如,另一个域)已打开。@DavidItarenco:您有相关的源代码吗?多年来,我一直成功地使用类似的代码重定向在框架中查看网站的用户的浏览器,没有任何问题。我很想知道这是否发生了变化。我发现了相互矛盾的源代码,所以我需要自己进行测试。我非常确定所有
.location
活动是跨域保护的(就iframe->parent操作而言),但可能我错了。谢谢你的回答,但我不理解这部分//创建cookie“opened”并将其设置为true。介意再解释一点吗?JavaScript可以使用属性document.cookie创建cookie、读取cookie和删除cookie。
    var domains = ["abc.net", "abc.com", "abc.info", "abc.blogspot.com"]; //urls that can be allowed
    if (domains.indexOf(document.location.hostname) == -1 && document.cookie.indexOf('opened=true') == -1) //checks if it is in the list of "domains"
{
        window.top.location = "http://abc.net"; //then redirects to "http://abc.net"
        document.cookie = "opened=true";  //creates cookie 'opened' and sets it to true
    }