You can do with a same link tag ( <a></a>
) and the confirm
function of JavaScript. Example:
<a href="https://www.google.com/" target="_blank" onclick="return confirm('Deseja realmente sair?');">Sair do site</a>
Detailing:
The onclick
event is added to the a
tag that returns the result of the confirm
function.
The confirm
function is native to JavaScript, and opens a window displaying the text passed as a parameter, an 'OK' button, and a 'Cancel' button. If the user clicks OK, the function returns true
. If the user clicks Cancel, the function returns false
.
When you return false
to the onclick
event, it stops executing the default action of the element, which in the case of the a
tag is the call to the link indicated in the href
attribute.
Note: Semantically, the most appropriate for this action would be the use of the <a></a>
tag, since by definition it indicates the link of the current page with another page. You can read more about setting and using the a
tag at: link