jquery - 监听 HTML 导航事件

标签 jquery dom navigation

使用 DOM API 和/或 JQuery,是否有任何方法可以检测用户何时启动导航事件(例如,通过单击具有有效 hrefa 元素) >),并为其绑定(bind)一个监听器,该监听器将在检索和呈现新 HTML 页面之前执行?

最佳答案

演示:http://jsfiddle.net/se8osjsj/1/

$(window).on("beforeunload", function() {
    // do stuff here
    return "You are leaving this page, are you sure?";
});

或者简单地说:

window.onbeforeunload = function(event) {
    // do stuff here
    return "You are leaving this page, are you sure?";
};

The onbeforeunload event occurs when the document is about to be unloaded.

This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page.

The default message that appears in the confirmation box, is different in different browsers. However, the standard message is something like "Are you sure you want to leave this page?". This message cannot be removed.

However, you can write a custom message together with the default message. See the first example on this page.

Note: If the onbeforeunload event is not assigned to the element, you must assign/attach the event on the window object, and use the returnValue property to create a custom message (see syntax examples below).

Note: In Firefox, only the default message will be displayed (not the custom message (if any)).

来源:http://www.w3schools.com/jsref/event_onbeforeunload.asp

关于jquery - 监听 HTML 导航事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29754768/

相关文章:

javascript - react 组件中的 jQuery

javascript - 停止传播委托(delegate)事件

jquery - 当网页嵌入到 Div 中时如何防止 Css 继承

javascript - 如何添加带有空白导航标签的 WordPress 菜单项?

javascript - 在javascript中仅返回给定字符串中的数字

jQuery DataTables 将数据发布到服务器

javascript - 我可以重用 DOM 元素吗?

jquery - 从 DOM 选择中排除元素及其后代

jquery - CSS 菜单下拉定位

jquery - 使用查询更改滚动百分比的 CSS