如何强制页面仅在IFrame中加载?

时间:2022-08-23 10:06:35

I have a set of asp.net pages which I wish they should only be accessible or loaded when they are loaded from an IFrame. If an attempt is made to access the pages directly via browser address bar then the page should not be displayed at all or display the message to the user.

我有一组asp.net页面,我希望它们只能在从IFrame加载时才能访问或加载。如果尝试直接通过浏览器地址栏访问页面,则根本不应显示该页面或向用户显示该消息。

I tried using cookies and sesions, but they are not that effective becuase once the cookie/session is created you can access the pages directly from browser, bypassing IFrame.

我尝试使用cookie和sesions,但它们并不是那么有效,因为一旦创建cookie /会话,您可以直接从浏览器访问页面,绕过IFrame。

My development platform is asp.net 2.0+, vs2008, C# 2.0+

我的开发平台是asp.net 2.0 +,vs2008,C#2.0+

2 个解决方案

#1


This is an example of one of the few times it is better to put the script in the head tag.

这是将脚本放在head标签中的几次之一的示例。

<html>
<head>
    <title>sandBox</title>
    <script type="text/javascript">
        if (frameElement == null) {
            //change location or close
            window.location = "http://*.com";
            // or window.close();
        }
    </script>
</head>
<body>
content goes here
</body>
</html>

#2


Try this inside your head tag:

在头标记内尝试:

<script>
if(window.self !== window.top); //inside an iframe
else window.location = "http://*.com/" // Outside
</script>

#1


This is an example of one of the few times it is better to put the script in the head tag.

这是将脚本放在head标签中的几次之一的示例。

<html>
<head>
    <title>sandBox</title>
    <script type="text/javascript">
        if (frameElement == null) {
            //change location or close
            window.location = "http://*.com";
            // or window.close();
        }
    </script>
</head>
<body>
content goes here
</body>
</html>

#2


Try this inside your head tag:

在头标记内尝试:

<script>
if(window.self !== window.top); //inside an iframe
else window.location = "http://*.com/" // Outside
</script>