怎么实现页面关闭前提示保存

donkey301 2011-03-22 10:01:07
在JS中加入:

window.onbeforeunload = function(evt) {
alert("onbeforeunload");
}

发现关闭网页时没有弹出alert,为什么?
我还测试了一下网上的代码,如下,也不行。

window.onbeforeunload = function(evt) {
if ($("#SmallChanged").val() == "true") {
evt = evt || window.event;
if (window.event) {
if (evt.clientX > document.documentElement.clientWidth && evt.clientY < 0 || evt.altKey)
evt.returnValue = "Do you want to leave this page without saving?";
}
else {
if (clientH < 222)
evt.returnValue = "Do you want to leave this page without saving?";
}
}
}

document.onmousemove = function(e) {
e = e || window.event;
if (e != window.event) {
clientW = e.pageX;
clientH = e.pageY;
}
}

...全文
211 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
浴火_凤凰 2011-03-23
  • 打赏
  • 举报
回复
window.onbeforeunload不能被重载吧
danxiaogui01 2011-03-23
  • 打赏
  • 举报
回复
window.onbeforeunload = function(){
if( event.clientX>document.body.clientWidth && event.clientY<0 || event.altKey ) {

}else if(event.clientY > document.body.clientHeight || event.altKey)
{
}
}
donkey301 2011-03-23
  • 打赏
  • 举报
回复
今天又试了一遍,onbeforeunload在Firefox下是可以的,但我现在用的是QT下的webkit关联的页面,这个函数就没有用了。
donkey301 2011-03-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zell419 的回复:]

HTML code

<script type="text/javascript">
var MSG_UNLOAD="您的文章内容还没有进行保存!";
var UnloadConfirm = {};
UnloadConfirm.set = function(confirm_msg){
window.onbeforeunload = function(event){
event = event ……
[/Quote]
不行,一样,没反应
donkey301 2011-03-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 a34020249 的回复:]

<script language=javascript>

window.onbeforeunload =function(){
if(event.clientX> document.body.clientWidth&&event.clientY <0||event.altKey)
{
alert( "确定要关闭吗 "); ……
[/Quote]
还是不行,没反应
zell419 2011-03-22
  • 打赏
  • 举报
回复

<script type="text/javascript">
var MSG_UNLOAD="您的文章内容还没有进行保存!";
var UnloadConfirm = {};
UnloadConfirm.set = function(confirm_msg){
window.onbeforeunload = function(event){
event = event || window.event;
event.returnValue = confirm_msg;
}
}
UnloadConfirm.clear = function(){
window.onbeforeunload = function(){};
}
UnloadConfirm.set(MSG_UNLOAD);
</script>
a34020249 2011-03-22
  • 打赏
  • 举报
回复
<script language=javascript>

window.onbeforeunload =function(){
if(event.clientX> document.body.clientWidth&&event.clientY <0||event.altKey)
{
alert( "确定要关闭吗 ");
}
}
</script>

87,902

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧