google-chromefirefoxpostmessagehtml5-fullscreen

requestFullscreen works no more in message event handler


User clicks a button and in click event handler we postMessage to iframe. Iframe handles it in message event handler and calls element.requestFullscreen(). In older browsers it worked (eg. in Chrome 65) but in current (72) it errors with Failed to execute 'requestFullscreen' on 'Element': API can only be initiated by a user gesture..

Is there a way to transfer "gesture initiated" flag in postMessage call?

Note that iframe has allow="fullscreen" attribute.


Solution

  • As always with iframes, it depends on here it is hosted relative to the parent document.

    If you are running a same-origin document in that frame, then you can simply call requestFullScreen directly on the element you wish form the main doc:

    const frameDoc = frame.contentDocument;
    frameDoc.getElementById('el-to-fs').requestFullscreen(); // assuming polyfill
    

    jsfiddle Since StackSnippetsĀ® overly protected iframes won't allow access nor fullscreen.


    But if you were, you wouldn't have had this issue in Chrome (you'd still have had in FF).

    Because even though it is true that this method requires an user-gesture, postMessage being fast enough, you are able to use it with a same-origin frame in Chrome.

    What happens in this browser is that a cross-origin document must have received an user-gesture before being able to call requestFullscreen.
    Once the cross-origin frame has been marked as interacted-by-the-user, then you'll be able to call requestFullscreen even from main-doc's events: jsfiddle (still, only in Chrome).

    But, for a cross-browser solution, you'd have to