我有一组 asp.net 页面,我希望它们只能在从 IFrame 加载时才能访问或加载。如果尝试通过浏览器地址栏直接访问页面,则该页面根本不应该显示或向用户显示消息。
我尝试使用 cookie 和会话,但它们并不是那么有效,因为一旦创建了 cookie/会话,您就可以绕过 IFrame 直接从浏览器访问页面。
我的开发平台是asp.net 2.0+、vs2008、C# 2.0+
这是少数几个最好将脚本放在 head 标记中的示例之一。
<html>
<head>
<title>sandBox</title>
<script type="text/javascript">
if (frameElement == null) {
//change location or close
window.location = "http://stackoverflow.com";
// or window.close();
}
</script>
</head>
<body>
content goes here
</body>
</html>
Use this JS in the page to check whether it is in iframe or not.
if(window == window.top) {
//page is not in an iframe
}
在你的head标签内试试这个:
<script>
if(window.self !== window.top); //inside an iframe
else window.location = "http://stackoverflow.com/" // Outside
</script>