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.