Categories
JavaScript Answers

How to add an onclick function to go to a URL in JavaScript?

Spread the love

Sometimes, we want to add an onclick function to go to a URL in JavaScript.

In this article, we’ll look at how to add an onclick function to go to a URL in JavaScript.

How to add an onclick function to go to a URL in JavaScript?

To add an onclick function to go to a URL in JavaScript, we set window.location to the URL we want to go to on the onclick function.

For instance, we write

window.location = url;

to set window.location to url to navigate to the URL in the url string.

Conclusion

To add an onclick function to go to a URL in JavaScript, we set window.location to the URL we want to go to on the onclick function.

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 *