Close current window and open new window

A common request!

To close the current window and open a new one, you can use the following methods:

Method 1: Using the window.close() method and window.open() method

  1. Close the current window: window.close()
  2. Open a new window: window.open('https://www.example.com', '_blank')

Method 2: Using the window.open() method with the replace parameter

  1. Open a new window: window.open('https://www.example.com', '_blank', 'replace=true')

Method 3: Using the window.location.replace() method

  1. Close the current window: window.location.replace('https://www.example.com')

Note: The window.close() method only works if the window was opened by JavaScript, and not if it was opened by the user or another script.

Also, the window.open() method with the replace parameter will replace the current window with the new one, whereas the window.location.replace() method will replace the current URL with the new one, but keep the same window.

Choose the method that best fits your use case!