Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.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,我不太懂js,但我创建了一个简单的脚本,允许只从某个页面访问某个页面,下面是代码: <script language="javascript" type="text/javascript"> if (document.referrer == "example111/index.html") { window.history.go(0); } else { window.location = "index.html"; } </script> if(document.r

我不太懂js,但我创建了一个简单的脚本,允许只从某个页面访问某个页面,下面是代码:

<script language="javascript" type="text/javascript">
if (document.referrer == "example111/index.html")
{
window.history.go(0);
}
else
{
window.location = "index.html";
}
</script>

if(document.referer==“example111/index.html”)
{
window.history.go(0);
}
其他的
{
window.location=“index.html”;
}
现在的问题是,我首先在一个虚拟网站上尝试了它,它成功了,但现在我把它移植到另一个网站上,它不起作用,它或者只是重定向到“index.html”;即使从我希望使用的页面查看,如果直接访问,它的问题是什么?

语法

string = document.referrer; 
注释

如果用户直接导航到页面(不是通过链接,而是通过书签),则该值为空字符串。由于此属性仅返回字符串,因此它不授予您对引用页面的DOM访问权

试试这个,行吗


if(document.referer.indexOf('index.html')=-1){
警报(“访问违规”);
顶部。位置=”http://www.yahoo.com";
}

确保将
参考者
与正确的字符串进行比较(部分匹配是不够的),然后尝试以下操作:

if (document.referrer !== "example111/index.html") {
    window.location.href = "index.html";
}

window.history.go(0)
。然后在刷新时,
else
执行其工作。

如果与错误的字符串进行比较,请检查的值。另外请注意,您可能会从
referer
获得一个空字符串。历史的概念是什么?go(0)?@Teemu我问过,但它是正确的。我尝试过“sitename.com/index.html”以及“index.html”。他们都在虚拟网站上工作,但现在,他们都产生了相同的结果。一个空字符串是什么意思?它应该报告其链接到右侧的页面?什么是
history.go(0)
?重新加载页面会改变
referer
@Teemu的值我在没有代码的情况下尝试过(使用alert(“”))但仍然存在同样的问题我不太理解你所说的,但这正是我想要的,如果用户直接或通过书签进入页面,我不希望他看到页面,我只希望在通过我页面上的链接导航时页面可见。@user3514931那么您应该使用
if(document.referer.split('/')[2]==“您的域名”)
go(0)仅在您已经浏览页面并通过任何链接或button@ArjunChaudhary第二个脚本正在运行,但是,如果直接访问页面,则会显示“访问冲突框”,但如果单击“确定”,则会在一瞬间看到所有信息,然后该框再次返回。window.location=“index.html”;使用此选项而不是警报框
if (document.referrer !== "example111/index.html") {
    window.location.href = "index.html";
}