What is the use of the open() method in JavaScript?

The open() method in JavaScript is used to open a new browser window or tab with a specified URL. It is a built-in method of the window object, which is the global object in the browser environment. The open() method takes three optional parameters: url, name, and specs.

The url parameter specifies the URL of the document to be displayed in the new window or tab. If this parameter is not specified, the new window or tab will be blank.

The name parameter specifies the name of the new window or tab. If this parameter is not specified, a default name will be used.

The specs parameter specifies a comma-separated list of window features, such as the size and position of the window, whether or not it is resizable, and whether or not it has a menu bar or status bar.

Here are some examples of usage of open() method:

Example 1. Open a new window with a specified URL

function openWindow() {
  window.open("http://www.example.com");
}

In this example, the openWindow() function calls the open() method with a URL of "http://www.example.com". When the function is called, a new window will be opened with the specified URL.

Example 2. Open a new window with a specified name

function openWindow() {
  window.open("http://www.example.com", "exampleWindow");
}

In this example, the openWindow() function calls the open() method with a URL of "http://www.example.com" and a name of "exampleWindow". When the function is called, a new window will be opened with the specified URL and name.

Example 3. Open a new window with specified window features

function openWindow() {
  var specs = "height=400,width=400,menubar=no,status=no";
  window.open("http://www.example.com", "exampleWindow", specs);
}

In this example, the openWindow() function calls the open() method with a URL of "http://www.example.com", a name of "exampleWindow", and a set of window features specified by the specs variable. The window features include a height and width of 400 pixels, no menu bar, and no status bar. When the function is called, a new window will be opened with the specified URL, name, and window features.

Example 4. Open a new tab instead of a new window

function openTab() {
  window.open("http://www.example.com", "_blank");
}

In this example, the openTab() function calls the open() method with a URL of "http://www.example.com" and a target of "_blank". The target "_blank" specifies that the link should open in a new tab instead of a new window. When the function is called, a new tab will be opened with the specified URL.

Example 5. Open a new window and set its content dynamically

function openWindow() {
  var newWindow = window.open("", "exampleWindow");
  newWindow.document.write("<h1>Hello, world!</h1>");
}

In this example, the openWindow() function calls the open() method with an empty URL and a name of "exampleWindow". This opens a new window with a blank document. The function then uses the document.write() method to dynamically set the content of the new window. In this case, it writes an h1 heading that says "Hello, world!". When the function is called, a new window will be opened with the specified name, and the content of the window will be dynamically set to "Hello, world!".

Example 6. Open a new window and navigate to a different URL dynamically

function openWindow() {
  var newWindow = window.open("", "exampleWindow");
  newWindow.location.href = "http://www.example.com";
}

In this example, the openWindow() function calls the open() method with an empty URL and a name of "exampleWindow". This opens a new window with a blank document. The function then sets the location.href property of the new window to "http://www.example.com". This causes the new window to navigate to the specified URL. When the function is called, a new window will be opened with a blank document, and then it will navigate to "http://www.example.com".

Example 7. Open a new window and close it after a specified time

function openWindow() {
  var newWindow = window.open("http://www.example.com", "exampleWindow");
  setTimeout(function () {
    newWindow.close();
  }, 5000);
}

In this example, the openWindow() function calls the open() method with a URL of "http://www.example.com" and a name of "exampleWindow". This opens a new window with the specified URL. The function then sets a timeout using the setTimeout() method. This causes the close() method to be called on the new window after 5 seconds (5000 milliseconds). When the function is called, a new window will be opened with the specified URL, and then it will be closed after 5 seconds.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: