How to Force the page to be loaded only within an IFrame?

岁酱吖の 提交于 2019-12-08 00:29:38

问题


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.

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.

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


回答1:


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

<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>



回答2:


Try this inside your head tag:

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


来源:https://stackoverflow.com/questions/1097117/how-to-force-the-page-to-be-loaded-only-within-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!