Detecting if it is loading an iframe or direct

I want to show only the form if it stretches on the page inside the iframe. How can I do it? Is there a server side solution?

+4
source share
4 answers

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

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

Which only hides the form with the identifier "myform" if the window is not the topmost window.

+5
source

I can’t think of a purely server-side method, but you can use some hybrid javascript / rails.

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

you could put some javascript in your head to check if it loads as an iframe, and if not, redirect to action and possibly display flash msg "can load this page only inside the application"

Javascript / head 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> 

pay attention to the parameter: iframe_fail, which you can check in the controller, and do anything if this parameter is present, for example. display flash message or redirect

sample controller

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

Not entirely beautiful, but it can help you get the job done.

+2
source

My iframe tag looked like

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

I wanted to hide the title of my webpage in this iframe, so I used code like:

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

If you notice, then contents () returns the element as "# document", which is an html iframe, so calling javascript will try to access your actual webpage made in the background of the iframe without it.

0
source

You can only verify this on the client side via JavaScript.

However: DO NOT DO IT . There are many legitimate purposes for placing a site in a (i) frame. If you violate such an iframe or in any way change your site in such circumstances, they will only make your users annoyed.

-5
source

Source: https://habr.com/ru/post/1332844/


All Articles