How to go to URL after OK button if alert is pressed with JavaScript?

Sometimes, we want to go to URL after OK button if alert is pressed with JavaScript.

In this article, we’ll look at how to go to URL after OK button if alert is pressed with JavaScript.

How to go to URL after OK button if alert is pressed with JavaScript?

To go to URL after OK button if alert is pressed with JavaScript, we use the confirm function.

For instance, we write

if (confirm("are you sure?")) {
  document.location = "http://example.com/";
}

to call confirm with the message string to display.

If we click OK, then true is returned and the code in the if block is run.

Conclusion

To go to URL after OK button if alert is pressed with JavaScript, we use the confirm function.