可能重复:
How can i get the destination url in javascript onbeforeunload event?
有没有办法在onunload事件中识别待处理的导航uri?我想根据用户在页面上导航的位置(特定链接,后退导航,刷新等)采取特定操作。是否有更好的方法来处理任务?
为了全面包容,我避免在链接中添加监听器,同样不想使用popstate,hashchange等等.onunload似乎是捕捉几乎任何场景的最简单方法,但是我没有我们能够找到一种方法来查看在此范围内加载新页面的位置。可以吗?
正如评论中所述,我并不完全同意这是上述帖子的副本。我绝对不认为它应该被关闭,因为现在没有人可以提供任何答案,包括我自己。无论如何,这是我使用的解决方案:
$(document).ready(function() {
var newUri;
$(document).on("click", "*", function(e) {
var thisElement = $(this)[0];
if (thisElement.onclick) {
// Handle special case situations.
}
if (thisElement.href) {
newUri = thisElement.href;
console.log(thisElement);
}
});
window.onbeforeunload = function(e) {
console.log("Client navigating to "+newUri);
// Perform final handling steps.
};
});