检测这是 iframe 加载还是直接加载

发布于 2024-10-08 09:47:47 字数 54 浏览 5 评论 0原文

我希望仅在 iframe 内的页面上拉出表单时才显示该表单。我该怎么做?有服务器端解决方案吗?

I am looking to only show a form if it is pulled on a page within an iframe. How do I do that? Is there a server side solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ヅ她的身影、若隐若现 2024-10-15 09:47:59

我的 iframe 标签就像


%iframe{:高度=> “98%”,:宽度=> "98%",:"id" => “个人资料Iframe”}

我想在此 iframe 中隐藏网页的标题,因此我使用的代码如下:


var $frame = $(window.parent.frames["profileIframe"]).contents();  
$frame.find('.header-ui').hide();

如果您观察到,contents() 返回一个元素为“#document”,它是 iframe 的 html,因此在没有此操作的情况下调用 javascript 将尝试访问在 iframe 背景中呈现的实际网页。

My iframe tag was like


%iframe{:height => "98%", :width => "98%",:"id" => "profileIframe"}

I wanted to hide header of my webpage within this iframe hence I used code as:


var $frame = $(window.parent.frames["profileIframe"]).contents();  
$frame.find('.header-ui').hide();

If you observe then contents() returns a element as "#document", which is html of iframe, hence calling a javascript without this will try to access your actual webpage rendered in background of iframe.

可爱咩 2024-10-15 09:47:59

您只能通过 JavaScript 在客户端进行检查。

但是:不要这样做。将网站放入 (i) 框架中有很多合法用途。在这种情况下突破此类 iframe 或以任何方式更改您的网站只会让您的用户生气不高兴。

You can only check it on the client side via JavaScript.

However: DO NOT DO THAT. There are plenty of legitimate uses of putting a site in a (i)frame. Breaking out of such iframe or changing your site in any way in such circumstances them will only make your users pissed unhappy.

风吹雨成花 2024-10-15 09:47:58

我想不出纯粹的服务器端方式,但你可以使用一些混合 javascript/rails。

假设您有一个专用的 iframe 布局模板,例如“layouts/iframe.erb”,

您可以在头部放置一些 javascript 来检查它是否作为 iframe 加载,如果不是,则重定向到一个操作并可能显示一个flash msg“只能在应用程序内加载此页面”

头部的javascript/rails

    <script type="text/javascript">

        function parentExists()
        {
         return (parent.location == window.location)? true : false;
        };

        function check_modal(){
        if (parentExists()) {
          window.location = '<%= url_for( :controller => "home", :action => 'iframe_action', :iframe_fail => 'true')%>'}
        }
        check_modal()
    </script>

注意参数:iframe_fail,您可以在控制器中检查该参数,如果该参数存在,则可以执行任何操作,例如显示flash msg 或重定向

示例控制器

def iframe_action
  if params[:iframe_fail]
         flash[:notice] = 'can only load inside app'
      else
       #do something else
      end
end

不太漂亮,但可能会帮助您完成工作。

I can't think of purely serverside way, but you could use a bit of hybrid javascript/rails.

assuming that you have a dedicated iframe layout template e.g. 'layouts/iframe.erb'

you could put some javascript in the head to check if it is being loaded as an iframe, and if it is not, redirect to an action and maybe display a flash msg "can only load this page inside application"

The javascript/rails for the head

    <script type="text/javascript">

        function parentExists()
        {
         return (parent.location == window.location)? true : false;
        };

        function check_modal(){
        if (parentExists()) {
          window.location = '<%= url_for( :controller => "home", :action => 'iframe_action', :iframe_fail => 'true')%>'}
        }
        check_modal()
    </script>

notice the param :iframe_fail which you could check for in a controller and do whatever you please if that param is present e.g. display flash msg or redirect

example controller

def iframe_action
  if params[:iframe_fail]
         flash[:notice] = 'can only load inside app'
      else
       #do something else
      end
end

Not real pretty but might help you get the job done.

走过海棠暮 2024-10-15 09:47:57

如果您使用的是 JQuery...(此处安装说明: http://jquery.com/

$(document).ready(function(){ 
    if( window == window.top) { $('form#myform').hide(); }
});

这只是隐藏了如果窗口不是最顶层的窗口,则表单的 ID 为“myform”。

If you are using JQuery... (installation instructions here: http://jquery.com/ )

$(document).ready(function(){ 
    if( window == window.top) { $('form#myform').hide(); }
});

Which just hides the form with id "myform" if the window is not the topmost window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文