8

Here's documentation on exiting fullscreen mode.

I used this code that I learnd to make the browser go fullscreen (it works), but my attempts to modify a version of it to exit fullscreen failed. Dealing with these non-standard APIs is a little tricky, with each browser implementing it a bit differently.

Here's the code:

// Bring the page into full-screen mode - Works!
function requestFullScreen(element) {

    // Supports most browsers and their versions.
    var requestMethod = element.requestFullScreen || 
        element.webkitRequestFullScreen           || 
        element.mozRequestFullScreen              || 
        element.msRequestFullScreen;
    if (requestMethod) {
        requestMethod.call(element);
    } else if ( typeof window.ActiveXObject !== "undefined") {
        var wscript = new ActiveXObject("WScript.Shell");
        if (wscript !== null) {
            wscript.SendKeys("{F11}");
        }
    }
}

// Exit fullscreen - Doesn't work!
function exitFullScreen(element){
    var requestMethod = element.exitFullscreen || 
        element.mozCancelFullScreen            || 
        element.webkitExitFullscreen           || 
        element.msExitFullscreen;
    if (requestMethod) {
        requestMethod();
    } else {
        console.log("Oops. Request method false.");
    }
}

And the calls:

var $fullscreenButton = $("#fullscreen-button");
var $smallscreenButton = $("#smallscreen-button");

$fullscreenButton.on("click", function() {
    var elem = document.body;

    // Make the body go full screen.
    requestFullScreen(elem);
});

$smallscreenButton.on("click", function() {
    var elem = document.body;

    // Exit full screen.
    exitFullScreen(elem);
});

What's wrong with the exitFullScreen function? How can I fix it?

Edit:

  • I'm working on a JSFiddle for this!
  • By "Doesn't work", I meant that it outputs "Oops. Request method false."
  • I'm calling the function exitFullScreen() with the parameter document.body

JSFiddle:

While the full screen request function works for me in the browser normally, I could not get it to work in JSFiddle, and I'm unsure whether this is because of my own mistake, or something to do with JSFiddle.

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
WebDeveloper404
  • 1,215
  • 3
  • 10
  • 11
  • 2
    Perhaps comment as to why the downvote? It's nearly impossible to ask a question on Stackoverflow these days, with everyone here down-voting like crazy, not suggesting improvements, and just attacking any question that isn't perfect, or generalized, or... whatever you people are looking for here these days. On other Stack Exchange communities, so many questions get up-votes and good feedback. Here, it's like the whole system has gone to hell. So many people respond negatively.. ***Just my observation :)*** – WebDeveloper404 Aug 04 '14 at 00:01
  • You're not supposed to ask "What's wrong with my code?" you are supposed to make a working example http://stackoverflow.com/help/mcve – Matthew Lock Aug 04 '14 at 00:13
  • 2
    @MatthewLock No, you're mistaken. Working examples are for Code Review.SE - Stack Overflow is for code that ***doesn't work***. This code is easily verifiable, has been simplified to my precise problem, and is completely straight-forward. – WebDeveloper404 Aug 04 '14 at 00:21
  • I think you missed this part from that link: "Complete – Provide all parts needed to reproduce the problem". You would get much more help if you created a MCVE as a jsfiddle and posted it here – Matthew Lock Aug 04 '14 at 00:25
  • So now every simple JavaScript Stack Overflow question requires a JSFiddle to be a good question? That's new... So what happens when someone posts a C++ question? Are they supposed to provide some service to compile the code and run it for fast testing? – WebDeveloper404 Aug 04 '14 at 00:27
  • From the link above: "When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem" you don't *have* to provide a jsfiddle but it helps – Matthew Lock Aug 04 '14 at 00:29
  • @MatthewLock And I agree that it helps. But I'm not getting requests for a JSFiddle. I'm getting ***down-votes***, which mean people are saying this is a *bad question* - Sure, any question can be improved, but how is this one a *bad question*? – WebDeveloper404 Aug 04 '14 at 00:31
  • 2
    Are you getting errors in the console? What browser(s) are you having the issue? I believe what @MatthewLock is reaching for is http://sscce.org/, which this is not. It's not required (AFAIK) but it certainly helps; users can downvote for whatever reason they want, so they aren't tied together. I also wouldn't get too worked into a lather by downvotes, it's just wasted effort. – Jared Farrish Aug 04 '14 at 00:32
  • Exactly. The time is better spent creating a quick jsfiddle. I've found jsfiddle invaluable in helping me debug by focusing on just the problem in question rather than in the context of the entire application. As to why Stackoverflow has become tougher to avoid downvotes? My guess is that so many low quality questions are posted that people have itchy downvote trigger fingers. – Matthew Lock Aug 04 '14 at 00:34
  • @MatthewLock - I'm not looking for a hallelujah chorus, it's just my opinion. The OP is justified to expect an explanation for downvotes; it's not really helpful to do so without explaining what could be improved to remove the downvote. The system is designed to work that way. Ranting about it isn't a great strategy, though. – Jared Farrish Aug 04 '14 at 00:40
  • 2
    @WebDeveloper404, "***doesn't work***" is not very helpful.. what does not work ? do you get errors ? do you get you own ˋconsole.logˋ message ? how are you calling those methods ? what arguments are you passing ? Which browsers exhibit the problem ? Each of those issues could be a reason for failure.. – Gabriele Petrioli Aug 04 '14 at 00:40
  • 2
    @GabyakaG.Petrioli I've added these details into the question. Thanks for the suggested improvements! – WebDeveloper404 Aug 04 '14 at 00:46
  • It appears to not work relating to the framing. Here is a URL to one that does: http://fiddle.jshell.net/4tD5M/5/show/light/ – Jared Farrish Aug 04 '14 at 01:12
  • When I log those four options to exit fullscreen (the functions), all are undefined (in Chrome). Are you sure that these exit methods exist? – Jared Farrish Aug 04 '14 at 01:17
  • @JaredFarrish I didn't downvote the OP, I was just seeking to help the OP understand why he might have got downvotes. Sorry for the "hallelujah chorus" – Matthew Lock Aug 04 '14 at 01:44
  • @MatthewLock - Sure; I wasn't saying you did (although it may sound that way, I was only pointing out the OP had a real grievance). The music is beautiful, all well. `:)` – Jared Farrish Aug 04 '14 at 01:47
  • For jsfiddle to work like that you need "No wrap" enabled. I've updated it for you http://jsfiddle.net/4tD5M/7/ notice now the logging statements are working in the console – Matthew Lock Aug 04 '14 at 01:52
  • Possible duplicate of https://stackoverflow.com/questions/23913111/firefox-browser-rejected-fullscreen-change – IgnoranceIsBliss Dec 29 '22 at 20:13

1 Answers1

5

For entering the fullscreen there were some issues with the capitalization.

For the exiting you need to call it on the document and not on the body, and you also need to apply it correctly instead of calling the reference to the method..

so requestMethod.call(element); for exiting as well.

See demo at http://jsfiddle.net/gaby/FGX72/show
(tested on latest IE/Chrome/FireFox)

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 2
    There might be a little bit of irony the OP got so much grief for the "lackluster" quality of the question, but your answer only demonstrates the solution in full while looking at the source of a break-out frame in a fiddle. – Jared Farrish Aug 04 '14 at 01:21
  • 1
    Can you explain why `.call()` is necessary? Since the methods are already attached to the element (`element.msExitFullscreen`), I don't understand why `requestMethod()` doesn't suffice. – WebDeveloper404 Aug 04 '14 at 17:22
  • @WebDeveloper404 because the executing function has a different context if you call its reference. – Gabriele Petrioli Aug 04 '14 at 17:27
  • For reference, the fiddle doesn't work because of this issue (from Firefox's console): _"Request for full-screen was denied because at least one of the document's containing iframes does not have an "allowfullscreen" attribute."_. So since the fiddle's in an iFrame, it doesn't work. – doppelgreener Feb 13 '15 at 06:22