Categories
JavaScript Answers

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

Spread the love

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.

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 *