【发布时间】:2011-02-13 09:40:39
【问题描述】:
我有一个 ASPX 页面,它有时会加载到我的应用程序的 iFrame 中,有时不会。从后面的代码中,您如何检测您的页面是否是从 iFrame 加载的。
【问题讨论】:
我有一个 ASPX 页面,它有时会加载到我的应用程序的 iFrame 中,有时不会。从后面的代码中,您如何检测您的页面是否是从 iFrame 加载的。
【问题讨论】:
您应该使用 JavaScript 来执行此操作。
【讨论】:
你不能在服务器端检测到这一点,在客户端这个 java-script 代码应该可以工作。
<script type="text/javascript">
if (top == self)
alert('Not in an iframe');
else
alert('In an iframe');
</script>
【讨论】:
我真诚地怀疑您是否可以从后面的代码中做到这一点。您可能可以在 JavaScript 中做到这一点,但不能从背后的代码中做到。
如果您真的需要从后面的代码中了解,您可能会在 iframe 中加载页面时提供一个 Query String 参数?比如……
<iframe src ="page.aspx?iframe=true" width="100%" height="300">
</iframe>
然后在后面的代码中你可以做类似...
// 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
}
【讨论】: