Java Script - handling multiple browser windows - onfocus

 

Very often when we are developing web applications there is a requirement to open additional Windows, typically named as popups or dialogbox. Many modern frontend libraries provides mechanisms to implement this kind of requirement using a combination of CSS and HTML components to create a graphical effect to provide a user experience similar to a new window.

But actually the native JS programming contains mechanisms to open additional browser Windows, and to keep to keep the control of the opened Windows.

This is the goal of this tutorial, to demonstrate how to open new windows and keep the track of these objects, aiming to implement a few typical requirements regarding the user interface.

First, to open a new window, we basically have use the method window.open. This method will open a new window: however, modern web browsers typically opens this window as a new tab, so if you want to have that opened in a popup/dialog box effect you should use the additional method parameters: first informing it has to be opened in a “_blank” target, and also specify its height and width properties. The method has a return value, which is a reference to the opened window. You can see an example of the open method in the block of code below.

        let newWindow =  null;
        function openNewWindowSingleInstance(){
            //.closed: a boolean attribute the informs if the window was closed or not
            if(newWindow !== null && !newWindow.closed){
                //.focus(): if the window is minimized, it becomes visible again
                newWindow.focus();
            }else{
                // the .open method returns a reference to the opened window
                newWindow = window.open(URL,"_blank","width=500,height=750");
            }
        }

 

In this example once the window was opened the variable newWindow keep the tracking of this object. The we can access its attributes, such as “closed” which is a boolean attribute which indicates the window still opened of already has been closed. This attribute is important, because somethings there is a functional requirement to keep just one instance of a window opened. In a certain way, it is like the Singleton design patter which aims to ensure that a certain class will just have an object instance of that.

Beyond the close attribute this example make usage of the .focus() method, which will make the referenced window visible to user, independently if it was minimized. It is important when you want to make visible an already existing window object, instead of create new ones.

 

You can download the how source code of this example here:

 https://github.com/rafaelqg/code/blob/main/multiple_windows_onfocus.html

You may watch a video class demonstrating how this code works in a live example:



Comments

Popular posts from this blog

HASHLIB: Using HASH functions MD5 and SHA256

Spread Operator

Dart: implementing Object Oriented Programming (OOP)