In a webpage I am creating I want to disconnect from a connection automatically upon the closing of the page, yet it refuses to work. The disconnect function exists in an external .js file that is correctly referenced in the head.
<script>
window.onbeforeunload = disconnect;
</script>
I previously changed it from "disconnect();" to "disconnect;". There was no change in result. Any help would be appreciated.
Because I have no idea what " disconnect " does, here is a simple example of how to bind a function to the window.onbeforeunload
event:
<script type="text/javascript">
// bind a function to the window.onbeforeunload event
window.onbeforeunload = function(event) {
// your disconnect() function
disconnect();
// bad ui, but for example - prompt user to verify they want to leave the page
prompt("Are you sure you want to leave this page?");
// log to the developer tools console that this event fired
console.log("onbeforeunload event fired");
};
</script>
Hopefully that helps. It looks like the problem may be in how you have disconnect()
defined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.