How to Redirect to a New URL
A JavaScript redirect uses the programming language JavaScript (JS) to send users from one URL to another.
You can use a JS redirect to redirect users to a confirmation page after they submit a form or to provide fallback redirection if a page gets deleted, for example.
But they are not generally not recommended for SEO because search engines can have difficulty crawling and indexing sites that rely on them.
In this article, we’ll discuss creating a redirect with JS and suggest alternatives.
How to Redirect with JavaScript
There are three main ways to redirect to another URL with JavaScript:
- window.location.href
- location.assign()
- location.replace()
Set a New window.location.href Property
You can use the window.location.href property in JavaScript to get or set the URL of the current webpage.
The use of “window.location.href” for redirection simulates the action of clicking on a hyperlink. It adds a new entry to the browser’s session history. This allows users to use the “back” button to return to the previous page.
To redirect a user to a different URL, you can assign a new URL to this property.
The syntax is:
window.location.href=‘https://exampleURL.com/’;
When tied to a user interaction-triggered change, such as a button click, this would redirect the user to “https://exampleURL.com/”
How?
You need to add the code inside of <script> tags inside of the <head> of your webpage and add an “event handler” to a button.
Here’s how you could do that:
...
source: https://news.oneseocompany.com/2023/08/07/how-to-redirect-to-a-new-url_2023080748606.html
Your content is great. However, if any of the content contained herein violates any rights of yours, including those of copyright, please contact us immediately by e-mail at media[@]kissrpr.com.