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
Release: 2024-11-11 06:42:03
Original
944 people have browsed it

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";
Copy after login

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";
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template