How to play a youtube video in an iframe? Refused to display '' in a frame because it is set 'X-Frame-Options' to 'SAMEORIGIN'

3

I'm trying to play a YouTube video on my Site using iframe and it's generating me the error that is in the title of the question.

HTML:

 <div class="video-container">
     <iframe
        src="https://www.youtube.com/watch?v=65Iz_GKbhHk"></iframe></div>

Web.Conf:

<system.webServer><httpProtocol><customHeaders><addname="X-Frame-Options" value="SAMEORIGIN" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

Note: I added the X-Frame-Options" value="SAMEORIGIN" option to the configuration file

  • What's wrong?
asked by anonymous 15.08.2015 / 18:02

1 answer

5

Just open the video on YouTube and click Share, just above the description. You will open a panel with the Embed option, click it. You will be presented with an HTML code, just copy and paste on your website. There you can still customize the player.

For example, for the video you passed, the generated code is this:

<iframe width="560" height="315" src="https://www.youtube.com/embed/65Iz_GKbhHk"frameborder="0" allowfullscreen></iframe>

Why does not your code work?

YouTube does not allow the entire page to be inserted into any site. It is only possible to make an iframe with the specific incorporation of the player, which is ideal.

It does this by inserting an HTTP header into the full page, x-frame-options: SAMEORIGIN , which instructs browsers not to load the page if the requestor is from a different domain. Learn more about this header in MDN . What you put in web.conf only prohibits other sites from placing your site in an iframe.

    
16.08.2015 / 06:40