Home >Web Front-end >JS Tutorial >What is the difference between `window.location.href` and `window.open()` in JavaScript?

What is the difference between `window.location.href` and `window.open()` in JavaScript?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-05 09:41:02234browse

What is the difference between `window.location.href` and `window.open()` in JavaScript?

Differences Between window.location.href and window.open() Methods in JavaScript

The distinction between window.location.href and window.open() lies in their nature and functionality.

window.location.href

  • Is not a method but a property.
  • Retrieves the current URL location of the browser.
  • Modifying its value redirects the current page.

Example:

<code class="javascript">window.location.href = "http://www.example.com"; // Redirects to example.com</code>

window.open()

  • Is a method that takes a URL argument.
  • Creates a new browser window or tab and navigates it to the specified URL.

Example:

<code class="javascript">window.open("http://www.google.com"); // Opens Google in a new window</code>

Additional Features

  • window.open() can take additional parameters to customize the new window, such as its size, location, and features (e.g., menubar, toolbar).
  • For more information on window.open() parameters, refer to the window.open tutorial.

The above is the detailed content of What is the difference between `window.location.href` and `window.open()` in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn