Detecting full screen or browser mode

As you've learned in the previous recipe, an application can run in full screen or in browser mode. By default, web applications will run on the second one, that is, in the browser mode. Because it's possible to run applications in these two modes, it could be useful to detect which mode is running. Let's explore how to do it.

How to do it...

  1. We're going to use only JavaScript code to detect the running mode for our application. You can use a simple HTML page with the following code inside the<head> section:
    <script type="text/javascript" charset="utf-8">
    var mode = 'browser';
    if (navigator.standalone) {
    mode = "standalone";
    }
    alert("App. running in " + mode + " mode");
    </script>
    

    For testing our JavaScript code, we'll add the following onload attribute to the <body> tag:

    <body onload="init()";>
    
  2. Don't forget to close the <body> and <html> tags properly. The complete code for this recipe can be found at code/ch03/modes.html in the code bundle provided on the Packtpub site.

How it works...

The navigator JavaScript object provided by Safari contains a special property called standalone. It allows you to detect the running mode of a web application. This recipe uses a simple alert box for displaying this information. However, you can write your own JavaScript function and invoke it when you need to detect this mode. Inside the function, we'll do something more sophisticated than displaying an alert box.

See also

  • Creating and customizing a notification box recipe in Chapter 2 ,Building Interfaces.
  • Viewing applications in full screen recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.134.151