How to redirect to another webpage in JavaScript?

September 28, 2022 No comments javascript redirect webpage

Introduction

In JavaScript, we may need to redirect to another webpage.

In this post, we'll look at how to redirect to another website in JavaScript.

How to redirect to another webpage in JavaScript

To redirect to another webpage using JavaScript we use window.location.replace(...) method or window.location.href = URL:

// similar behavior as an HTTP redirect
window.location.replace("http://example.com");

// similar behavior as clicking on a link
window.location.href = "http://example.com";

Note that replace() won't create an entry in your browser's history.

Conclusion

To redirect to another webpage in JavaScript we can use location.replace() or window.location.href = URL.

{{ message }}

{{ 'Comments are closed.' | trans }}