Here’s a function I wrote while working on a project that pops up a box confirming whether or not the user is sure the want to go to a link they clicked, like this.
It may be simple, but I haven’t really used javascript’s confirm() function much in the past and everything I found only directed the user to a link predefined in the function. I wanted a function that would confirm any link I wanted it to without knowing the link destination in advance, in other words I wanted to confirm dynamic links.
The Code
This jQuery function will pop up a confirmation box on any link with the class ‘confirm’ and send the user to the destination of that link if they click ‘OK’.
$(document).ready(function() {
$('a.confirm').click(function(event) {
event.preventDefault()
var url = $(this).attr('href');
var confirm_box = confirm('Are you sure you want to click this link?');
if (confirm_box) {
window.location = url;
//uncomment below and remove above if you want the link to open in a new window
//window.open(url,'_blank');
}
});
});
Code Explained
//load once document is ready
$(document).ready(function() {
//select all anchors with a class of 'confirm'
$('a.confirm').click(function(event) {
//prevent the default action of opening the link
event.preventDefault()
//grab the url of the current link
var url = $(this).attr('href');
//set the confirmation message
var confirm_box = confirm('Are you sure you want to click this link?');
//if the confirmation is true (user clicks ok) direct the browser to the link's url
if (confirm_box) {
window.location = url;
//uncomment below and remove above if you want the link to open in a new window
//window.open(url,'_blank');
}
});
});
Example
Because the link below has a class of ‘confirm’ you’ll have to confirm whether or not you want to go to the link’s destination.
Link Code
<a href="http://www.grimmdude.com/contact/" class="confirm">Link Example</a>

Hello,
I searched for three days to get this…. thanks so much.
One question though: How could I get the confirmed link to launch in a new tab?
Thanks again,
Brad
Garrett,
Thanks. I’ve actually implemented this by globally adding the css class, “external” to all external links and they already include the _blank target. However, this isn’t working — upon confirm it simply proceeds to the link in the same tab/window. Any ideas?
Thanks again.
brad
Ah ok, yea disregard what I said before. The script doesn’t use the target attribute from the link since it’s just calling
window.location.Try using this script instead:
It works exactly the same, but uses
window.open()to open a new window.Perfect! That did the trick. Thanks so much.
Because of a jQuery conflict, I’m using it like this:
jQuery(document).ready(function() {
jQuery(‘a.external’).click(function(event) {
event.preventDefault()
var url = jQuery(this).attr(‘href’);
var confirm_box = confirm(‘confirm text here’);
if (confirm_box) {
window.open(url,’_blank’);
}
});
});
good night,
I have a doubt … if I have more than one link with the class he will execute the first to find?