惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
D
Docker
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
美团技术团队
V
V2EX
Last Week in AI
Last Week in AI
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
B
Blog RSS Feed
博客园_首页
B
Blog
博客园 - 叶小钗
I
InfoQ
WordPress大学
WordPress大学
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
The Register - Security
The Register - Security
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Latest news
Latest news
W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News and Events Feed by Topic
S
Secure Thoughts
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News

博客园 - cobbles

递归和迭代(Recursion and Iteration) 基本查找算法 位、字节、字长、频率、时钟周期、带宽 缓冲区 套接字同步与异步 原码、补码和反码 confluence [转]Groovy和Grails简介 [转]关于BI的一个故事 参考书目 [网址]db2管理 [转]用于数据仓库的 DB2 产品 [转]把GridView的内容输出到Excel的两点注意 将DataGrid生成excel word 访问IIS元数据库失败 asp调用Web Service Typed Vs. Untyped DataSets Typed DataSets in .NET Database Normalization Basics
[转]用javascript判断窗口关闭事件
cobbles · 2008-02-14 · via 博客园 - cobbles

function window.onbeforeunload()
{
if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey||event.ctrlKey)
{
     //判断event.altKey是为了Alt+F4关闭的情况;判断event.ctrlKey是为了Ctrl+W关闭的情况
     //document.body.clientWidth不包括滚动条,而关闭按钮恰好在滚动条右侧。
     window.event.returnValue="";
}
}
</script>
1.浏览器不是软件的窗口,要该事件非常困难,因为关闭与刷新这两个事件就很难区分。
2.都没有对应的事件,只能根据一些条件来判断,但未必准确。
3.前进、后退、刷新、关闭都响应事件window.onbeforeunload。
4.javascript之所以能得到某些事件属性和调用方法,是因为浏览器对象为他提供了接口,用COM的术语来说,是IDispatch接口,如果浏览器对象没有为他提供接口,他什么也干不了。所以有些功能在其它语言中可以实现,而在javascript中是永远无法实现的。


用onUnload方法
在body 标签里加入onUnload事件
body onUnload="myClose()"
然后在javascript里定义myClose()方法,但是onUnload方法是在关闭窗口之后执行,不是在关闭窗口之前执行,如果你想在关闭窗口之前做判断,请用第一种方法