Add Confirmation to Links

Sometimes there are situations where one might require a confirmation prior to leaving a site or maybe you just want a confirmation prior to executing an action.  This can be achieved very easily with a few lines of jQuery code.

Let’s assume we have the following code:


<a href="http://www.jquery.com" alt="jQuery - External Link" class="confirmexit" >jQuery</a>

You will notice that I have gave the link a class of confirmexit, this will be used as the jQuery trigger for the confirmation message. Below is the jQuery code that will be required.


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("a.confirmexit").click(function() {
return confirm("Are you sure you want to exit?")
})
})
</script>

Below is a working example:

jQuery with Message
jQuery without Message

line
footer
Network: Thomas Craig Consulting | Designed by Elegant Themes