Window.open() This method is used to open the web pages into new window. Syntax:
window.open(URL, name, specs, replace)
Parameters: This method accepts four parameters as mentioned above and described below:
URL: It is an optional parameter. It is used to set the URL of web pages which need to open. If URL is not set then window.open() method open a blank window.name: It is an optional parameter and used to set the window name.specs: It is an optional parameter and used to separate the item using comma.replace: It is an optional parameter and used to specify the URL URL creates a new entry or replaces the current entry in the history list. This parameter return boolean value. If this parameter returns true then URL replaces the current document in the history list and if returns false then URL creates a new entry in the history list.
Return Value: This method creates a new window.
Window.close() This method is used to close the window which are opened by window.open()method. Syntax:
window.close()
Parameters: This method does not contains any parameter.
Return Value: This method does not return any value.
Below example illustrates the window.open() and window.close() method in jQuery:
Example:
<!DOCTYPE html> <html> <head> <title> window open and close method </title> <script> var Window; // Function that open the new Window function windowOpen() Window = window.open( "https://www.coderssrta.com/", "_blank", "width=400, height=450"); } // function that Closes the open Window function windowClose() { Window.close(); } </script> </head> <body> <button onclick="windowOpen()"> Open CodersArts </button> <button onclick="windowClose()"> Close GeeksforGeeks </button> </body> </html>
Been searching for a while the shortest code to open new windows using Javascript, which was a part of my college assignment. Ended up with asking this site to do my coding homework. How could I know that weeks later I'll find a free and pretty workable solution meeting all criteria of my assignment? 😥