【问题标题】:How do you tell if your page is running in an iFrame in .Net? [duplicate]您如何判断您的页面是否在 .Net 中的 iFrame 中运行? [复制]
【发布时间】:2011-02-13 09:40:39
【问题描述】:

我有一个 ASPX 页面,它有时会加载到我的应用程序的 iFrame 中,有时不会。从后面的代码中,您如何检测您的页面是否是从 iFrame 加载的。

【问题讨论】:

    标签: c# .net html iframe


    【解决方案1】:

    您应该使用 JavaScript 来执行此操作。

    【讨论】:

      【解决方案2】:

      你不能在服务器端检测到这一点,在客户端这个 java-script 代码应该可以工作。

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

      【讨论】:

        【解决方案3】:

        我真诚地怀疑您是否可以从后面的代码中做到这一点。您可能可以在 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
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-03
          • 2010-10-28
          相关资源
          最近更新 更多