32229

Making YouTube player go fullscreen via JavaScript

Question:

I'm running into a bug when making the YouTube player to go into fullscreen mode via JavaScript.

I have a button that when it's clicked, it triggers the YT.Player.playVideo() method, while at the same time asking the containing iframe to go into fullscreen as follows:

thisVid = document.getElementById("ytIframe"); if (thisVid.requestFullscreen) { thisVid.requestFullscreen(); } else if (thisVid.msRequestFullscreen) { thisVid.msRequestFullscreen(); } else if (thisVid.mozRequestFullScreen) { thisVid.mozRequestFullScreen(); } else if (thisVid.webkitRequestFullScreen) { thisVid.webkitRequestFullScreen(); }

(This is using Stack Overflow answers <a href="https://stackoverflow.com/questions/7615380/auto-full-screen-for-a-youtube-embed" rel="nofollow">here</a> and <a href="https://stackoverflow.com/questions/17286787/on-play-make-youtube-video-full-screen" rel="nofollow">here</a>)

The iframe successfully takes up the screen, however, the YouTube player doesn't know it's in fullscreen mode because the fullscreen button in the bottom-right is still available: <img alt="Fullscreen still available" class="b-lazy" data-src="https://i.imgur.com/8Y4Dvwa.jpg" data-original="https://i.imgur.com/8Y4Dvwa.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

When I click it, it stays in fullscreen, and it tells me "youtube.com is now fullscreen":<img alt="Now YouTube is fullscreen" class="b-lazy" data-src="https://i.imgur.com/zMLdTDE.jpg" data-original="https://i.imgur.com/zMLdTDE.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

This is a problem because the user needs to click the button twice to be able to exit fullscreen mode. Is there a way to tell the youtube player to go fullscreen, instead of just telling its containing iframe to do so?

Answer1:

Because the YouTube player actually exists inside an iFrame, when you use the javascript fullscreen API you're full screening the iframe, not the player. So as far as it's concerned it is not full screen. The only way to get the player to go to full screen mode is for the user to click the button manually (this is a legacy requirement, as the iframe could still contain the Flash player, and Flash has security measures to prevent programmatic fullscreening). For now your only solution is a custom control bar (and even then you won't be full screening the player, but just its container ... but at least you'd have control over what the buttons do).

Recommend

  • How to exit fullscreen onclick using Javascript?
  • 2 Questions / 1. Fullscreen(jQuery) / 2. Background change and save as Cookies
  • web scrape with rvest
  • PyArg_ParseTuple SegFaults in CApi
  • Botframework - resize bot chat in width [duplicate]
  • SUM and GROUP BY in xquery with 1 xml file
  • How does the bind operator for Eval in Control.Parallel.Strategies evaluate its argument strictly?
  • ngFor youtube links with Domsanitizer in Angular2
  • ImportError: cannot import name COMError in python
  • get data from firestore firebase
  • Optional spaces and multiple alternatives in wl-pprint-extras
  • Flink: How to write DataSet to a variable instead of to a file
  • Send Raw Email (with attachment) to Multiple Recipients
  • Why wont Cygnus keep running?
  • JS object key sequence
  • Is it possible to create a self-extracting archive which will run a specific file when unzipped?
  • Remove previous Directions Route Google Maps
  • $_POST for text in DIV elements
  • Express displaying mongodb documents in Jade
  • Reading XML into Datatable gives incorrect DateTime when the time has Time Zone info
  • Does the Azure table storage API cache results?
  • what is the purpose of “export as namespace foo”?
  • Javascript Array, Object, Date not defined
  • Making Google Visualization - Annotation Chart to work in GWT
  • Can't locate Module/Build.pm in @INC (@INC contains: /usr/local/lib64/perl5
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • Update Google Maps traffic layer without page reloading
  • perl, mysql - fasting way to upload a csv file into mysql?
  • Elasticsearch script query involving root and nested values
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • Not able to aggregate on nested fields in elasticsearch
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Detect when Facebook like button is clicked
  • Chart.js Multiple dataset
  • How to attach a node.js readable stream to a Sendgrid email?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • JSON with duplicate key names losing information when parsed
  • Change an a tag attribute in JavaScript based on screen width
  • How to set the response of a form post action to a iframe source?
  • How to Embed XSL into XML