Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed HTML5 YouTube video without iframe?

Is it possible to embed an html5 version of a youtube video without using an iframe?

like image 303
user2217162 Avatar asked Sep 10 '13 18:09

user2217162


People also ask

Can you embed a YouTube video without an iframe?

One of the simplest methods to embed a YouTube video in a web page without IFrame is by embedding the video using the HTML <object> tag. Simply provide the URL of the video to the <object> element's data property and set few other properties like, the width, height, and you are ready to go.

Can you embed without iframe?

Use the embed Tag as an Alternative to Iframe in HTML We can embed media like PDF, image, audio, video, and web pages. The tag defines a container in which we can embed our desired content. The embed tag is a self-closing tag. We can use the src attribute to specify the URL of the web page to be embedded.

What can I use instead of iframe in HTML5?

As others have mentioned you can also use the embed tag and the object tag but that's not necessarily more advanced or newer than the iframe. HTML5 has gone more in the direction of adopting web APIs to get information from cross domains.


2 Answers

Here is a example of embedding without an iFrame:

<div style="width: 560px; height: 315px; float: none; clear: both; margin: 2px auto;">   <embed     src="https://www.youtube.com/embed/J---aiyznGQ?autohide=1&autoplay=1"     wmode="transparent"     type="video/mp4"     width="100%" height="100%"     allow="autoplay; encrypted-media; picture-in-picture"     allowfullscreen     title="Keyboard Cat"   > </div>

compare to regular iframe "embed" code from YouTube:

<iframe   width="560"   height="315"   src="https://www.youtube.com/embed/J---aiyznGQ?autoplay=1"   frameborder="0"   allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"   allowfullscreen> </iframe>

and as far as HTML5 goes, use <object> tag like so (corrected):

<object   style="width: 820px; height: 461.25px; float: none; clear: both; margin: 2px auto;"   data="http://www.youtube.com/embed/J---aiyznGQ?autoplay=1"> </object>
like image 189
anapsix Avatar answered Oct 06 '22 05:10

anapsix


Yes. Youtube API is the best resource for this.

There are 3 way to embed a video:

  • IFrame embeds using <iframe> tags
  • IFrame embeds using the IFrame Player API
  • AS3 (and AS2*) object embeds DEPRECATED

I think you are looking for the second one of them:

IFrame embeds using the IFrame Player API

The HTML and JavaScript code below shows a simple example that inserts a YouTube player into the page element that has an id value of ytplayer. The onYouTubePlayerAPIReady() function specified here is called automatically when the IFrame Player API code has loaded. This code does not define any player parameters and also does not define other event handlers.

<div id="ytplayer"></div>  <script>   // Load the IFrame Player API code asynchronously.   var tag = document.createElement('script');   tag.src = "https://www.youtube.com/player_api";   var firstScriptTag = document.getElementsByTagName('script')[0];   firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);    // Replace the 'ytplayer' element with an <iframe> and   // YouTube player after the API code downloads.   var player;   function onYouTubePlayerAPIReady() {     player = new YT.Player('ytplayer', {       height: '390',       width: '640',       videoId: 'M7lc1UVf-VE'     });   } </script> 

Here are some instructions where you may take a look when starting using the API.


An embed example without using iframe is to use <object> tag:

<object width="640" height="360">     <param name="movie" value="http://www.youtube.com/embed/yt-video-id?html5=1&amp;rel=0&amp;hl=en_US&amp;version=3"/     <param name="allowFullScreen" value="true"/>     <param name="allowscriptaccess" value="always"/>     <embed width="640" height="360" src="http://www.youtube.com/embed/yt-video-id?html5=1&amp;rel=0&amp;hl=en_US&amp;version=3" class="youtube-player" type="text/html" allowscriptaccess="always" allowfullscreen="true"/> </object> 

(replace yt-video-id with your video id)

JSFIDDLE

like image 30
Ionică Bizău Avatar answered Oct 06 '22 03:10

Ionică Bizău