Javascript: Validate action before redirection from html link

A straightforward way to inform the user about something before using an html link is using the `confirm` javascript function. This function is a browser element that will print a simple text message giving two posibilities ‘Cancel’ an ‘OK’. The way it is rendered depends on the browser client used as well as the language used in the response buttons.

If we want to inform the user about something before sending him to the url we can do it like this.

Imagine an html link


<a href="url_to_redirect" onclick='confirm("Are you sure about this?")'>Anchor text</a>

Replace the content of the onclick attribute with any JS script.

Leave a Reply

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