How do you tell if your page is running in an iFrame in .Net? [duplicate]
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.
精彩评论