gpt4 book ai didi

jquery - 监听 HTML 导航事件

转载 作者:行者123 更新时间:2023-12-01 03:37:35 27 4
gpt4 key购买 nike

使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com