如何阻止直接访问html页面

Moh*_*amy 2 html

我有一个子 html (模板)products.html,我index.html使用 JavaScript/jquery 函数 .load() 将其包含在我的页面中

现在的问题是,如果你输入products.html的url,就可以直接访问它,而且它给出的页面非常难看,所以我尝试使用JS函数将任何试图访问它的人重定向到index.html windows.location。这种方法的问题是,当我的模板加载到我的主页中时,js 脚本会触发,并导致页面刷新。那么还有其他方法可以解决这个问题吗?

Sha*_*yal 5

window.parentPage = true; 您可以像在index.html 文件中一样定义变量。

在 products.html 页面中进行如下检查:

if(!window.parentPage)
{
    window.location.href = "YOUR REDIRECTION PAGE"
}
Run Code Online (Sandbox Code Playgroud)