开发者

How do you tell if your page is running in an iFrame in .Net? [duplicate]

开发者 https://www.devze.com 2022-12-28 06:24 出处:网络
This question already has answers here: Detect if a page is within a iframe - serverside 开发者_C百科(5 answers)
This question already has answers here: Detect if a page is within a iframe - serverside 开发者_C百科 (5 answers) Closed 9 years ago.

I have an ASPX page that is sometimes loaded into an iFrame in my application and sometimes not. From the code behind how can you detect if your page is being loaded from an iFrame.


I sincerely doubt you could do that from the code behind. You could probably do that in JavaScript, but not from the code behind.

If you really need to know from the code behind, you would perhaps provide a Query String parameter when you load the page in the iframe? Something like...

<iframe src ="page.aspx?iframe=true" width="100%" height="300">
</iframe>

Then in code behind you could do something like...

// sometimes my Asp.NET, Java and PHP get a little confused
// so maybe this might throw some no such index type errors,
// but I think it communicates the idea
string iframe = Request.QueryString["iframe"];
if(!string.IsNullOrEmpty(iframe) && iframe.Equals("true"))    {
    // this page is in the iframe
}


You can't detect this on server-side, on the client-side this java-script code should work.

<script type="text/javascript">
    if (top == self)
        alert('Not in an iframe');
    else
        alert('In an iframe');
</script>


You should use JavaScript to do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号