Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect when an iFrame goes fullscreen?

I have a document that is embedded in my site by using an iFrame. The iFrame is from Box.com document viewer. The iFrame has its own built in fullscreen button. The fullscreen button is within the iFrame so I cannot attach a click event listener to the button. I added the attribute allowfullscreen to the iFrame to allow it to go fullscreen.

I want to do something like this:

$('iframe').on 'EnterFullScreen', () ->
  # Run function

But what event do I have to listen to, to detect when the iFrame is going fullscreen?

Here is a jsfiddle with the type of document I am embedding. The goal is to detect when the document goes fullscreen.

http://jsfiddle.net/Rnvcm

like image 879
Nearpoint Avatar asked Jun 24 '14 14:06

Nearpoint


People also ask

Can iframe go fullscreen?

The ” iframe ” tag defines a rectangular region within the document in which the browser can display a separate document, including scrollbars and borders. An inline frame is used to embed another document within the current HTML document. For the fullscreen Iframe, you have to cover the entire viewport.

How do I know if my browser is in full screen?

The fullscreenElement property tells you the Element that's currently being displayed in fullscreen mode on the DOM (or shadow DOM). If this is null , the document (or shadow DOM) is not in fullscreen mode. The fullscreenEnabled property tells you whether or not it is possible to engage fullscreen mode.


2 Answers

You can listen for a fullscreen change in your parent page (the one having the iframe):

function changeHandler(e) {
   // Mode has changed.
}

document.addEventListener("fullscreenchange", changeHandler, false);
document.addEventListener("webkitfullscreenchange", changeHandler, false);
document.addEventListener("mozfullscreenchange", changeHandler, false);
like image 151
putvande Avatar answered Oct 13 '22 01:10

putvande


Using the events pointed out by putvande you can still bind your own enterFullScreen event:

$(document).on('fullscreenchange mozfullscreenchange webkitfullscreenchange msfullscreenchange', function() {
    if (document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen || document.msFullscreenElement)
    {
        $(document).trigger('enterFullScreen');
    }
    else
    {
        $(document).trigger('leaveFullScreen');
    }
});

You can use the simpler enterFullScreen event now using:

$(document).on('enterFullScreen', function(){
    // Do stuff
});
like image 22
user887675 Avatar answered Oct 13 '22 01:10

user887675