How to Redirect to Another Webpage using JavaScript?

How to Redirect to Another Webpage using JavaScript?

JavaScript can redirect the users from current page to another webpage (different URL) with and without requiring any manual action (like clicking a link). This can be achieved in JavaScript using the window.location object and it will allow us to redirect programmatically.

Using window.location.href

The window location href property is used to set or return the full URL of the current page. The Location href property can also be used to set the href value redirect to another link. The Location href property returns a string that contains the entire URL of the page, including the protocol (http, https, etc). 

Syntax

window.location.href = "URL"

Using location.replace() Method

The location replace() method is used to replace the current document with the specified one. This method is different from the assign() method as this removes the current document from the document history, therefore it is not possible to go back to the previous document using the ‘back’ button. 

Syntax

location.replace("URL");

How to redirect browser window back using JavaScript ?

There are two approaches used to redirect the browser window back:

  • Using history.back() Method
  • Using history.go() Method

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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