Home >Web Front-end >JS Tutorial >How do I redirect users to a different page in JavaScript?

How do I redirect users to a different page in JavaScript?

Barbara Streisand
Barbara StreisandOriginal
2024-11-11 06:42:03973browse

How do I redirect users to a different page in JavaScript?

Redirecting with JavaScript

Redirecting users to a different page from the current one is a common task in web development. In JavaScript, you can achieve this using the window.location property.

Solution:

To redirect to a new page, use the following code:

window.location = "http://www.yoururl.com";

This code assigns the new URL to the window.location property, effectively directing the user to the specified page.

Example:

To redirect to "example.com", you would write:

window.location = "example.com";

Note:

  • Replace "http://www.yoururl.com" with the desired URL.
  • The code works for both absolute and relative URLs. Absolute URLs specify the full URL, including the scheme (e.g., "http://"), while relative URLs are relative to the current page's URL.

The above is the detailed content of How do I redirect users to a different page 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