jQuery: Control page reloads or moving away from the current page
If a user moves away from the current page by using mouse (clicking on a link) or keyboard (hitting enter while having a form element focused) interactions, i want to do be able to run JavaScript/jQuery commands beforehand.
Exemplary workflow:
- User clicks on any link or hits a key which开发者_开发知识库 will result in loading a new page in the browser
- A JavaScript event gets fired and does something (for example
console.log()
) - Finally, the page load is executed
Furthermore, it would be great if that also works with user events that trigger a page reload (like pressing F5).
I realize that there is something similar here on Stackoverflow. It shows an alert before loading another page if there is unsaved content in an answer box. That may be the same functionality i ask for.
$( window ).unload(function() {
return "Handler for .unload() called.";
});
Reference: JQuery unload().
精彩评论