什么元素触发页面退出
我试图找到触发页面退出的元素。假设他们点击了 a href...我将把 JS 发起的位置更改留给另一场战斗。我想在退出之前做一些处理(即保存应用程序的状态)。
我尝试过的方法:
$(window).bind("unload", function(e) { console.log("====EXIT===="); console.log($(this)); console.dir(e); } );
$(this) 和“e”(事件)都没有引用导致它的元素。
- $(this) 什么也没有
- ,“e”打印一个空对象“c.Event”
I'm trying to find the element that triggered a page exit. Say they click on an a href... I'll leave the JS-initiated location changes for another battle. I want to do some processing before exit (ie, save state of app).
What I've tried:
$(window).bind("unload", function(e) { console.log("====EXIT===="); console.log($(this)); console.dir(e); } );
Neither the $(this) nor "e" (event) reference the element that caused it.
- $(this) is nothing
- and "e" prints an empty object "c.Event"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我强调这个对你有用。
尝试这些;
i pressume this one will work for you.
Try with theese;
JavaScript 事件是“onBeforeUnload”,其对应的 jQuery 事件是“beforeunload”:
编辑 -
确定哪个元素导致 URL 更改的唯一方法是挂钩页面上的所有元素。例如:
然后您可以使用
lastElementClicked
对象的属性 - 例如href
、data()
或html() - 在
beforeUnload
中执行逻辑。The JavaScript event is "onBeforeUnload" and its corresponding jQuery event is 'beforeunload':
EDIT -
The only way to determine which element caused a URL change is to hook all of the elements on the page. For example:
You can then use the
lastElementClicked
object's attributes -- such ashref
,data()
orhtml()
-- in thebeforeUnload
to do your logic.