JavaScript Page Redirection

please click here for more wordpress cource

In JavaScript, you can use the window.location object to redirect to another page. Here’s an example:

// Redirect to a new page
window.location.href = "https://example.com";

// Redirect to a new page without keeping the current page in the session history
window.location.replace("https://example.com");

The window.location.href property sets the URL of the current page to the specified URL, causing the browser to load a new page.

The window.location.replace() method works similarly, but it doesn’t keep the current page in the session history, so the user can’t use the back button to navigate back to it. Instead, the new page becomes the new entry in the history stack.

You can also use window.location.assign() method to redirect to a new page, but it behaves the same way as window.location.href.

// Redirect to a new page using assign
window.location.assign("https://example.com");

Note that the URLs must be in the same domain as the current page to avoid security issues, unless the server specifically allows cross-domain redirection.

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *