如何限制用户访问单独标签中的iframe?

时间:2018-07-22 16:57:35

标签: html redirect iframe

  

在我的索引页面上,有两个用于登录和注册的链接以及一个iframe

我分别设计了(登录和注册)页面 现在,我想强制两个页面仅在iframe中打开,而不是在单独的标签中打开 并且如果有人尝试直接访问,则将他们重定向到索引页面。 这是我的代码...

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<a href="login.php" target="frame">Login</a> <a href="signup.php">Signup</a>
<iframe src="" name="frame" frameborder="0"></iframe>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您可以将以下内容添加到HTMl文档的head中,以使其在未加载到iframe中时自动重定向:

<script>
window.onload = function(){
   if (top === self) {
     //not inside iframe
     location = 'url';
   }
}
</script>

JSFiddle:http://jsfiddle.net/xjqyrsd7/

答案 1 :(得分:0)

  

我刚刚找到了使用Javascript的最佳方法。

var myUrl = 'http://localhost/test/index.php';
if(window.top.location.href !== myUrl) {
    window.top.location.href = myUrl;
}
相关问题