Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to trigger JavaScript function on page reload? [closed]

Is it possible to trigger a JavaScript function when a user tries to reload the current page?

like image 848
Justin John Avatar asked Oct 18 '25 18:10

Justin John


2 Answers

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/

like image 91
Joseph Avatar answered Oct 20 '25 06:10

Joseph


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.');
}); 
like image 20
Rafay Avatar answered Oct 20 '25 06:10

Rafay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!