检测这是 iframe 加载还是直接加载
我希望仅在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的 iframe 标签就像
%iframe{:高度=> “98%”,:宽度=> "98%",:"id" => “个人资料Iframe”}
我想在此 iframe 中隐藏网页的标题,因此我使用的代码如下:
如果您观察到,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:
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.
您只能通过 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.
我想不出纯粹的服务器端方式,但你可以使用一些混合 javascript/rails。
假设您有一个专用的 iframe 布局模板,例如“layouts/iframe.erb”,
您可以在头部放置一些 javascript 来检查它是否作为 iframe 加载,如果不是,则重定向到一个操作并可能显示一个flash msg“只能在应用程序内加载此页面”
头部的javascript/rails
注意参数:iframe_fail,您可以在控制器中检查该参数,如果该参数存在,则可以执行任何操作,例如显示flash msg 或重定向
示例控制器
不太漂亮,但可能会帮助您完成工作。
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
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
Not real pretty but might help you get the job done.
如果您使用的是 JQuery...(此处安装说明: http://jquery.com/ )
这只是隐藏了如果窗口不是最顶层的窗口,则表单的 ID 为“myform”。
If you are using JQuery... (installation instructions here: http://jquery.com/ )
Which just hides the form with id "myform" if the window is not the topmost window.