Tips for exiting a web app that is taking up your entire screen

We're currently working on making our web app run in full screen mode. We were able to achieve this by implementing these meta tags:

    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <meta name="apple-mobile-web-app-title" content="Full Screen">

Now, whenever the web app is launched from the home screen of an iPhone or Android device, it will open in full screen mode without any browser controls.

Our next step is to provide users with a way to exit the app. I was considering creating a menu with a quit button, but using window.close() resulted in an error message in Chrome:

Scripts may close only the windows that were opened by it.

What would be the proper approach to address this issue?

Answer №1

If you want to exit full screen mode, you can do so by using the following script:

 function closeFullScreenMode(element) { 
        var requestMethod = element.exitFullscreen ||
                                                  element.mozCancelFullScreen || 
                                                  element.webkitExitFullscreen || 
                                                  element.msExitFullscreen; 
         if (requestMethod) { 
               requestMethod.call(element); 
         } else { 
               console.log("Error: Request method not found."); 
          } 
     }

To use this script, simply call it like this:

       var $exitButton = $("#exit-button"); 
       $exitButton.on("click", function() { 
                    var elem = document;
                    closeFullScreenMode(elem); 
        });

An issue that may arise with using window.close() is that you need to open the window first using window.open(). This allows JavaScript to then correctly close the window.

For example:

 var newWindow = window.open();
 newWindow.close(); // this will work.

Sources:

[1] How can we programmatically enter and exit the fullscreen mode in javascript?

[2] window.close() doesn't work - Scripts may close only the windows that were opened by it

Answer №2

Check out this helpful guide to learn about using the HTML5 fullscreen api

To exit full screen mode, you can use the following code:

if (document.exitFullscreen) {
    document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
    document.webkitExitFullscreen();
} else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
} else if (document.msExitFullscreen) {
    document.msExitFullscreen();
}

You can implement the above code in a click method of any DOM element, for example using jQuery:

$("#exitFullScreen").click(function(){
    // implementation of the exit fullscreen code
        if (document.exitFullscreen) {
        document.exitFullscreen();
    } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
    }
});

For further information, you can explore these additional resources:

  1. https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode

  2. http://davidwalsh.name/fullscreen

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with creating a variable name inside a JavaScript for loop

Here is the code snippet I am currently working on: var var1 = 58; for(var i=0;i<10;i++){ if(("var"+i) == 58) { console.log("they are equal"); } } I am wondering why ("var" + i) is not equal to 58 in this scenario. Could someone please provide an ...

Can the start and stop times of the typed.js plugin be controlled for typing text?

The typed.js jQuery plugin creates the illusion of text being automatically typed on screen by a robot. Despite researching the resources related to this plugin, I have not come across any information on how to control the starting and stopping of the typi ...

What are the steps to reinitialize Grunt in a Yeoman project?

My Grunt installation seems to be causing a lot of errors out of nowhere. I'm using Yeoman to scaffold my app, but today when I run Grunt Test, I get the following error messages: Loading "autoprefixer.js" tasks...ERROR >> Error: Cannot find mo ...

Adding a Key Value pair to every object within an Array using TypeScript

I have two arrays - one contains dates and the other contains objects. My goal is to include the dates as a key value pair in each object, like this: {"Date": "10-12-18"}. dates: ["10-12-18", "10-13-18", 10-14-18"] data: [ {"name":"One", "age": "4"} ...

Learn how to prevent two-finger swipe forward/backward on a trackpad using JavaScript or HTML

My app includes a functionality where the URL changes when transitioning from one state to another, for example from home/#state to home/#state2. However, I noticed that when I perform a two-finger swipe using the trackpad from home/#state2, the page navig ...

Issue with Bootstrap modal not closing

I've encountered an issue with a bootstrap modal popup. When I try to close the popup, it doesn't behave as expected. Instead of just hiding the popup and removing the backdrop, it hides the popup but adds another backdrop, making the screen almo ...

What is the best way to reset an event back to its original state once it has been clicked on again

As a newcomer to web development, I'm currently working on creating my own portfolio website. One of the features I am trying to implement is triangle bullet points that can change direction when clicked - kind of like an arrow. My idea is for them to ...

Tips for calculating the difference between timestamps and incorporating it into the response using Mongoose

In my attendance model, there is a reference to the class model. The response I receive contains two createdAt dates. const attendanceInfo = await Attendance.find({ student: studentId, }) .populate('class', 'createdAt'); ...

What is the best way to attach labels to objects in three.js when clicking the mouse?

Is it possible to apply a label to an object in a 3D scene using the three.js library through an onMouseClick event? ...

Combining Mouseover and Click Events in Vue JS

Having four pictures, I want to display a specific component when hovering over them. However, I also need to bind the click event so that clicking on the picture will reveal the component. The challenge is that I am unable to simultaneously bind two event ...

Combining two objects in Typescript using the spread operator and creating a reusable type

Is there a more streamlined way to dynamically add a question mark to a variable type in TypeScript, or is the approach of rewriting the type with a question mark the best way to achieve this? I'm looking to ensure that the original variables remain r ...

Is it possible to leverage both functions and variables within the ng-options expression in Angularjs?

I am faced with a situation where I have 2 select boxes. The first one is used to choose the user type (such as groups or individual), and the second one displays the options based on the selection made in the first box. I was wondering if it is possible t ...

"Encountering a jQuery JavaScript issue when utilizing the $(...) element

I am in the process of migrating some older code to jQuery: let xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { $("#" + ajaxArea).html(xmlHttp.responseText); $("#" + ajaxArea).attr('t ...

Guide to adding and showing records without the need to refresh the webpage using CodeIgniter

Hey there! I've got a code snippet here for inserting and displaying records without refreshing the web page using AJAX and plain PHP. However, I'm not sure how to set this up using CodeIgniter. Can someone please lend a hand? Here's what I ...

How to delete rows from a table in bootstrap with JavaScript

My goal is to create a table that allows users to delete specific rows using JavaScript and bootstrap. I attempted the standard method, but it doesn't seem to be working: <form action="scrivi.php" method="POST"> <t ...

Add value to a progress bar over time until it reaches the designated timeout

I'm struggling to implement a loading bar with a fixed timeout requirement. The goal is for the bar to be completely filled within 5 seconds. While I have successfully created the HTML and CSS components, I am facing difficulty in developing the JavaS ...

Creating evenly spaced PHP-generated divs without utilizing flexbox

My goal is to display images randomly from my image file using PHP, which is working fine. However, I am facing an issue with spacing the images evenly to fill the width of my site. This is how it currently appears: https://i.stack.imgur.com/AzKTK.png I ...

Exploring every conceivable method for accessing a file from a distant server

I am striving to maximize the flexibility of my script, thus I am seeking all potential methods in PHP and JavaScript to access the content (and not the source code) of a PHP file from a remote server. I have come across CURL, fopen, and include for PHP ...

Retrieve data from a REST API in a dynamic manner without manually specifying the table structure in the HTML code

I am looking to retrieve JSON data via a post request from a REST API: http://localhost/post1 param1='1' The response will look like this: { "json_table": [ { "date": 123, "test": "hello2" }, { "date": 19, ...

ExpressJS exhibits unique behavior based on whether the API is requested with or without the specified PORT number

I have encountered an issue with my 2 flutter web apps. One of them is functioning flawlessly when I request the URL, but the other one only works when I include the port xxxxx:4000/nexus-vote. However, when I remove the port, I receive a status code of 20 ...