- //原理很简单,就是在body的onbeforeunload事件绑定函数,代码如下:
- document.body.onbeforeunload = function (event)
- {
- var c = event || window.event;
- if (/webkit/.test(navigator.userAgent.toLowerCase())) {
- return "离开页面将导致数据丢失!";
- }
- else
- {
- c.returnValue = "离开页面将导致数据丢失!";
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0701201511508.html
来源: http://www.codesnippet.cn/detail/0701201511508.html