HomeLanguagesJavascriptHow to center a popup window on screen?

How to center a popup window on screen?

JavaScript window.open() method is used to open a popup window. This popup window will place into the center of the screen.

  • popupWinHeight: The height of pop up window on the screen.
  • popupWinWidth: The width of pop up window on the screen.

Example 1: This example creates the pop up window without placing it into center. 

html




<!DOCTYPE html>
<html>
     
<head> 
    <title> 
        Non-centered popup window
        on the screen
    </title>
</head>
 
<body>
    <h1>neveropen</h1>
     
    <p>
        Non-centered popup window
        on the screen
    </p>
     
    <script>
        function createPopupWin(pageURL, pageTitle,
                    popupWinWidth, popupWinHeight) {
            var left = (screen.width ) ;
            var top = (screen.height ) ;
            var myWindow = window.open(pageURL, pageTitle,
                    'resizable=yes, width=' + popupWinWidth
                    + ', height=' + popupWinHeight + ', top='
                    + top + ', left=' + left);
        }
    </script>
     
    <button onclick = "createPopupWin('https://www.geeksforgeeks.org',
            'neveropen Website', 1200, 650);">
        neveropen
    </button>
</body>
 
</html>


Output:

  • Before Clicking the button:
  • After Clicking the button:

Center popup window: To center the popup window we are changing the values of parameters of open() method as follows:

  • left = (screen.width – popupWinWidth) / 2
  • top = (screen.height – popupWinHeight) / 4

Example 2: This example creates the pop up window and placing it into center. 

html




<!DOCTYPE html>
<html>
     
<head> 
    <title> 
        Centered popup window
        on the screen
    </title>
</head>
 
<body>
    <h1>neveropen</h1>
     
    <p>
        Centered popup window
        on the screen
    </p>
     
    <script>
        function createPopupWin(pageURL, pageTitle,
                    popupWinWidth, popupWinHeight) {
            var left = (screen.width - popupWinWidth) / 2;
            var top = (screen.height - popupWinHeight) / 4;
             
            var myWindow = window.open(pageURL, pageTitle,
                    'resizable=yes, width=' + popupWinWidth
                    + ', height=' + popupWinHeight + ', top='
                    + top + ', left=' + left);
        }
    </script>
     
    <button onclick = "createPopupWin('https://www.geeksforgeeks.org',
            'neveropen Website', 1200, 650);">
        neveropen
    </button>
</body>
 
</html>


Output:

  • Before Clicking the button:
  • After Clicking the button:
RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS