If you want to use the onclick attribute directly in your HTML and prevent the default action of an anchor tag (or any other element) if the user confirms their action, you can create a JavaScript function that handles the confirmation and prevents the default action based on the user's response.
Example HTML
<a href="https://example.com" onclick="return confirmAndPrevent(event, 'Are you sure you want to leave this page?');" class="confirm-link">Link 1 - Confirm Before Leaving</a>
<a href="https://example.org" onclick="return confirmAndPrevent(event, 'Are you sure you want to leave this page?');" class="confirm-link">Link 2 - Confirm Before Leaving</a>
<a href="https://example.net" onclick="return confirmAndPrevent(event, 'Are you sure you want to leave this page?');" class="confirm-link">Link 3 - Confirm Before Leaving</a>
JavaScript Function
Here's the JavaScript function that handles the confirmation and prevents the default action:
function confirmAndPrevent(event, message) {
// Show a confirmation dialog
var confirmation = confirm(message);
if (!confirmation) {
// If the user clicked 'Cancel', prevent the default action
event.preventDefault();
return false; // Prevent default action, which includes following the link
}
// If confirmed, the link will be followed (return true)
return true;
}
Explanation of the Code
onclick Attribute: The onclick attribute on each anchor tag calls the confirmAndPrevent function and passes the event object and a confirmation message.
- Function Definition:
- The
confirmAndPrevent function shows a confirmation dialog using confirm().
- If the user clicks "Cancel" (i.e., the confirmation is false), it calls
event.preventDefault() to prevent the default link action and returns false.
- If the user clicks "OK", it returns
true, allowing the default action to take place (which in this case is to navigate to the link's URL).
Usage
With this approach, you can easily manage confirmation for specific links while keeping your HTML straightforward. The links will navigate away if the user confirms their choice; otherwise, no action will be taken.
Example in Full
Here's how it all comes together:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Confirm Link Example</title>
</head>
<body>
<a href="https://example.com" onclick="return confirmAndPrevent(event, 'Are you sure you want to leave this page?');" class="confirm-link">Link 1 - Confirm Before Leaving</a>
<a href="https://example.org" onclick="return confirmAndPrevent(event, 'Are you sure you want to leave this page?');" class="confirm-link">Link 2 - Confirm Before Leaving</a>
<a href="https://example.net" onclick="return confirmAndPrevent(event, 'Are you sure you want to leave this page?');" class="confirm-link">Link 3 - Confirm Before Leaving</a>
<script>
function confirmAndPrevent(event, message) {
var confirmation = confirm(message);
if (!confirmation) {
event.preventDefault();
return false; // Prevent default action
}
return true; // Allow default action
}
</script>
</body>
</html>
This implementation allows users to decide whether to proceed with the link action, improving user experience and reducing accidental navigation.