Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

javascript - 检测浏览器或标签页关闭(Detect browser or tab closing)

是否有跨浏览器的JavaScript / jQuery代码来检测浏览器或浏览器选项卡是否已关闭,但不是由于单击链接而导致的?

  ask by cometta translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I get you correctly, you want to know when a tab/window is effectively closed.

(如果我正确地理解了您,您想知道何时有效关闭标签页/窗口。)

Well, AFAIK the only way in Javascript to detect that kind of stuffs are onunload & onbeforeunload events.

(好吧,AFAIK是Javascript中检测此类内容的唯一方法是onunloadonbeforeunload事件。)

Unfortunately (or fortunately?), those events are also fired when you leave a site over a link or your browsers back button.

(不幸的是(或幸运的是?),当您通过link或浏览器的后退按钮离开站点时,也会触发这些事件。)

So this is the best answer I can give, I don't think you can natively detect a pure close in Javascript.

(因此,这是我能给出的最佳答案,我认为您无法自然地在Javascript中检测到纯close 。)

Correct me if I'm wrong here.

(如果我错了,请纠正我。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...