Is it possible to trigger a JavaScript function when a user tries to reload the current page?
you mean confirm an "unload" whereby execute a function before the user leaves the current page.
<html>
<head>
<script type="text/javascript">
function mymessage()
{
//this is the sample function code executed on "unload"
alert("This message was triggered from the onunload event")
}
</script>
</head>
<body onunload="mymessage()">
<p>close this window</p>
</body>
</html>
OR
the jQuery version http://api.jquery.com/unload/
it difficult to infer what exactly do you want but as you stated in the comments that you want a function to execute before the page gets refreshed you can use unload
$(window).unload(function() {
alert('Handler for .unload() called.');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With