How to make the window full screen with Javascript (stretching all over the screen)

后端 未结 19 1831
天涯浪人
天涯浪人 2020-11-21 22:13

How can I make a visitor\'s browser go fullscreen using JavaScript, in a way that works with IE, Firefox and Opera?

相关标签:
19条回答
  • 2020-11-21 22:35

    This is as close as you can get to full screen in JavaScript:

    <script type="text/javascript">
        window.onload = maxWindow;
    
        function maxWindow() {
            window.moveTo(0, 0);
    
            if (document.all) {
                top.window.resizeTo(screen.availWidth, screen.availHeight);
            }
    
            else if (document.layers || document.getElementById) {
                if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth) {
                    top.window.outerHeight = screen.availHeight;
                    top.window.outerWidth = screen.availWidth;
                }
            }
        }
    </script> 
    
    0 讨论(0)
  • 2020-11-21 22:35

    In newer browsers such as Chrome 15, Firefox 10, Safari 5.1, IE 10 this is possible. It's also possible for older IE's via ActiveX depending on their browser settings.

    Here's how to do it:

    function requestFullScreen(element) {
        // Supports most browsers and their versions.
        var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
    
        if (requestMethod) { // Native full screen.
            requestMethod.call(element);
        } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript !== null) {
                wscript.SendKeys("{F11}");
            }
        }
    }
    
    var elem = document.body; // Make the body go full screen.
    requestFullScreen(elem);
    

    The user obviously needs to accept the fullscreen request first, and there is not possible to trigger this automatically on pageload, it needs to be triggered by a user (eg. a button)

    Read more: https://developer.mozilla.org/en/DOM/Using_full-screen_mode

    0 讨论(0)
  • 2020-11-21 22:35

    Here is a complete solution to get in and out of full screen mode (aka cancel, exit, escape)

            function cancelFullScreen() {
                var el = document;
                var requestMethod = el.cancelFullScreen||el.webkitCancelFullScreen||el.mozCancelFullScreen||el.exitFullscreen||el.webkitExitFullscreen;
                if (requestMethod) { // cancel full screen.
                    requestMethod.call(el);
                } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
                    var wscript = new ActiveXObject("WScript.Shell");
                    if (wscript !== null) {
                        wscript.SendKeys("{F11}");
                    }
                }
            }
    
            function requestFullScreen(el) {
                // Supports most browsers and their versions.
                var requestMethod = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen;
    
                if (requestMethod) { // Native full screen.
                    requestMethod.call(el);
                } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
                    var wscript = new ActiveXObject("WScript.Shell");
                    if (wscript !== null) {
                        wscript.SendKeys("{F11}");
                    }
                }
                return false
            }
    
            function toggleFullScreen(el) {
                if (!el) {
                    el = document.body; // Make the body go full screen.
                }
                var isInFullScreen = (document.fullScreenElement && document.fullScreenElement !== null) ||  (document.mozFullScreen || document.webkitIsFullScreen);
    
                if (isInFullScreen) {
                    cancelFullScreen();
                } else {
                    requestFullScreen(el);
                }
                return false;
            }
    
    0 讨论(0)
  • 2020-11-21 22:36

    Here is my full solution for Full Screen and Exit Full Screen both (many thanks to help from tower's answer above):

    $(document).ready(function(){
    $.is_fs = false;
    $.requestFullScreen = function(calr)
    {
        var element = document.body;
    
        // Supports most browsers and their versions.
        var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
    
        if (requestMethod) { // Native full screen.
            requestMethod.call(element);
        } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript !== null) {
                wscript.SendKeys("{F11}");
            }
        }
    
        $.is_fs = true;    
        $(calr).val('Exit Full Screen');
    }
    
    $.cancel_fs = function(calr)
    {
        var element = document; //and NOT document.body!!
        var requestMethod = element.exitFullScreen || element.mozCancelFullScreen || element.webkitExitFullScreen || element.mozExitFullScreen || element.msExitFullScreen || element.webkitCancelFullScreen;
    
        if (requestMethod) { // Native full screen.
        requestMethod.call(element);
        } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript !== null) {
                wscript.SendKeys("{F11}");
            }
        }    
    
        $(calr).val('Full Screen');    
        $.is_fs = false;
    }
    
    $.toggleFS = function(calr)
    {    
        $.is_fs == true? $.cancel_fs(calr):$.requestFullScreen(calr);
    }
    
    });
    

    // CALLING:

    <input type="button" value="Full Screen" onclick="$.toggleFS(this);" />
    
    0 讨论(0)
  • 2020-11-21 22:39

    In Firefox 10, you can make the current page go fullscreen (real fullscreen with no window chrome) using this javascript:

    window.fullScreen = true;
    
    0 讨论(0)
  • 2020-11-21 22:41

    Luckily for unsuspecting web users this cannot be done with just javascript. You would need to write browser specific plugins, if they didn't already exist, and then somehow get people to download them. The closest you can get is a maximized window with no tool or navigation bars but users will still be able to see the url.

    window.open('http://www.web-page.com', 'title' , 'type=fullWindow, fullscreen, scrollbars=yes');">

    This is generally considered bad practice though as it removes a lot of browser functionality from the user.

    0 讨论(0)
提交回复
热议问题