Most browsers don't display the custom message passed to confirm()
.
With this method, you can show a popup with a custom message if your user changed the value of any <input>
field.
You can apply this only to some links, or even other HTML elements in your page. Just add a custom class to all the links that need confirmation and apply use the following code:
$(document).ready(function() {_x000D_
let unsaved = false;_x000D_
// detect changes in all input fields and set the 'unsaved' flag_x000D_
$(":input").change(() => unsaved = true);_x000D_
// trigger popup on click_x000D_
$('.dangerous-link').click(function() {_x000D_
if (unsaved && !window.confirm("Are you sure you want to nuke the world?")) {_x000D_
return; // user didn't confirm_x000D_
}_x000D_
// either there are no unsaved changes or the user confirmed_x000D_
window.location.href = $(this).data('destination');_x000D_
});_x000D_
});
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
_x000D_
_x000D_
<input type="text" placeholder="Nuclear code here" />_x000D_
<a data-destination="https://en.wikipedia.org/wiki/Boom" class="dangerous-link">_x000D_
Launch nuke!_x000D_
</a>
_x000D_
Try changing the input value in the example to get a preview of how it works.