There are several ways to redirect a user to a new page using JavaScript.
window.location.href
object:This is the most common method, as it is suitable for most uses:
window.location.href = "http://www.example.com";
This will redirect the user to "http://www.example.com" immediately.
window.location.replace()
method:window.location.replace("http://www.example.com");
This method replaces the current document with a new one. The difference between this method and the window.location.href
method is that replace()
doesn't create a new entry in the browser's history, so the user won't be able to use the "Back" button to navigate to the original document.
window.location.assign()
method:window.location.assign("http://www.example.com");
This method loads a new document into the current window or frame. It creates a new entry in the browser's history, so the user can use the "Back" button to navigate to the original document.
window.open()
method:window.open("http://www.example.com", "_self");
This method opens a new browser window or tab, depending on the user's browser settings. The "_self" parameter tells the browser to open the new page in the current window or tab. If you want to open the new page in a new window or tab, you can use "_blank" instead.