Categories
JavaScript Answers

How to change the current URL with JavaScript?

Spread the love

Sometimes, we want to change the current URL with JavaScript.

In this article, we’ll look at how to change the current URL with JavaScript.

How to change the current URL with JavaScript?

To change the current URL with JavaScript, we set the document.location.href property to the new URL.

For instance, we write

document.location.href = newUrl;

to set document.location.href to newUrl to navigate to the new URL.

Conclusion

To change the current URL with JavaScript, we set the document.location.href property to the new URL.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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