jQuery: Control page reloads or moving away from the current page

流过昼夜 提交于 2019-12-08 07:06:31

问题


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:

  1. User clicks on any link or hits a key which will result in loading a new page in the browser
  2. A JavaScript event gets fired and does something (for example console.log())
  3. 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.


回答1:


$( window ).unload(function() {
 return "Handler for .unload() called.";
});

Reference: JQuery unload().



来源:https://stackoverflow.com/questions/7511396/jquery-control-page-reloads-or-moving-away-from-the-current-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!