You're looking for confirm()
. Try this:
$('.external').click(function (e) {
var r = confirm("You are now leaving site");
if (r == true) {
// user agreed, do something
} else {
// user did not agree, do something else
}
e.preventDefault();
});
EDIT 1:
As per @JohnCarpenter, regarding localStorage
: Storing Objects in HTML5 localStorage
EDIT 2:
To redirect the page upon confirmation, use something like this:
window.location.replace("http://domain.com");
You could also get fancy with jQuery and get the URL dynamically from the original anchor's href
attribute:
window.location.replace($(this).prop('href'));
Please also see this post regarding window.location.replace
. It's possible that window.location.href
would better suit your needs.