javascript - 如何在javascript中限制直接url文件名?

标签 javascript jquery html

我需要在 javascript 中限制直接 URL 文件名!当我输入 HTML 文件直接 URL 时,它不应允许显示其中的内容。它应该向用户发出警报消息。仅当我们单击索引文件链接时它才有效。

文件是1.index.html 2.viewer.html

我尝试限制viewer.html 中的直接访问,但它不起作用。当我单击 index.html 页面中的链接时,查看器页面应该显示其内容。

Viewer.html代码

<iframe src="https://docs.google.com/viewerng/viewer?url=http:files/tutorial.pdf&chrome=true" style="width:100%; height:100%;" frameborder="0"></iframe>
<script type="text/javascript">

var accepted_domains=new Array("viewer.html","viewer.html")

var domaincheck=document.location.href //retrieve the current URL of user browser
var accepted_ok=false //set acess to false by default

if (domaincheck.indexOf("http")!=-1){ //if this is a http request
    for (r=0;r<accepted_domains.length;r++){
        if (domaincheck.indexOf(accepted_domains[r])!=-1){ //if a match is found
            accepted_ok=true //set access to true, and break out of loop
            break
        }
    }
}
else
    accepted_ok=true

if (!accepted_ok){
    alert("You\'re not allowed to directly link to this file on our server!")
    history.back(-1)
}
</script>

最佳答案

实现此目的的一种方法是使用“document.referrer”属性。

如果它是您的入口点,则 document.referrer 将设置为“”。

如果您链接到子页面,那么您的文档引荐来源网址将设置为您的 host/entry.html

http://www.w3schools.com/jsref/prop_doc_referrer.asp

不如服务器端,但也有一些。

关于javascript - 如何在javascript中限制直接url文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893561/

相关文章:

javascript - 使用 JQuery Draggable sortable 和 div

html - 图像导航栏悬停

html - 将 flex 容器内的元素向右移动

Javascript,显示透明图像。

javascript - 如何在 JavaScript 的 'if' 语句中指定多个条件

javascript - jQuery .animate 函数不起作用

javascript - 如何按名称引用带有 [] 括号的元素?

jQueryMobile : Hide data-icon for anchor tag

jQuery 用户界面 : when using multiple buttonsets (radio-buttons) they forget their state

c# - 替换邮件链接