3

How can I detect in a view function that the page is being loaded in an iFrame? I want to change the page slightly if that is the case. Is there any way to do this?

Note, Im not looking for click jacking protection. I want to make changes to my view if the page is being loaded in an iframe.

darkhorse
  • 8,192
  • 21
  • 72
  • 148
  • Do you have access to client code? – dvnguyen Jan 03 '18 at 23:26
  • @dvnguyen Nope, I don't – darkhorse Jan 03 '18 at 23:30
  • No chance - the server just get an request to deliver the page. Without passing additional information on client side you can't react on it. – dahrens Jan 03 '18 at 23:44
  • @dahrens Oh wait, I think I misunderstood. Yes, I have access to the client code. Im guessing you mean the template for the view right? If you mean the webpage requesting my view using the iFrame, then no, I don't. – darkhorse Jan 03 '18 at 23:57
  • You need to control the client code of the website that requests you through the iframe to be able to pass an HTTP header or GET parameter that indicates: I'd like the iframe represenation, pls. – dahrens Jan 04 '18 at 00:48
  • You might do something in JS when your website was loaded in an iframe. https://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t – dahrens Jan 04 '18 at 00:51
  • 2
    Possible duplicate of [How to identify if a webpage is being loaded inside an iframe or directly into the browser window?](https://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t) – solarissmoke Jan 04 '18 at 02:59

1 Answers1

2

You can check the {{ request.META.HTTP_SEC_FETCH_DEST }} variable in the template. Example:

<section id="header" class="hero is-small is-info {% if request.META.HTTP_SEC_FETCH_DEST != 'document' %}is-hidden{% endif %}">

List of possible values for HTTP_SEC_FETCH_DEST: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Dest