I have a page where the user can drag and drop objects and save them as an image.When a user navigates away from the page, the event beforeunload is fired. Now, this happens every time. What i want to do is, unbind the event if the user has saved his work, so that the message may not pop up again.To do this i have used the unbind method in jQuery. But, it does not seem to work. Below is the code for binding and unbinding the events.
我有一个页面,用户可以拖放对象并将其保存为图像。当用户离开页面时,会触发事件beforeunload。现在,每次都会发生这种情况。我想要做的是,如果用户保存了他的工作,解除绑定事件,以便消息可能不会再次弹出。为此,我在jQuery中使用了unbind方法。但是,它似乎不起作用。下面是绑定和解除绑定事件的代码。
var notSaved = function()
{
return 'You have not yet saved your work.Do you want to continue? Doing so, may cause loss of your work' ;
}
$(window).bind('beforeunload', notSaved);
After save method has been called,
调用save方法后,
$(window).unbind('beforeunload', notSaved);
What am i doing wrong here?
Also, the save method is actually an Ajax call.
我在这做错了什么?另外,save方法实际上是一个Ajax调用。
1 个解决方案
#1
30
beforeunload
doesn't work reliably this way, as far as binding goes. You should assign it natively:
就绑定而言,beforeunload不能以这种方式可靠地工作。你应该原生地分配它:
window.onbeforeunload = function() {
return 'You have not yet saved your work.Do you want to continue? Doing so, may cause loss of your work' ;
}
And in your save method:
在您的保存方法中:
window.onbeforeunload = null;
#1
30
beforeunload
doesn't work reliably this way, as far as binding goes. You should assign it natively:
就绑定而言,beforeunload不能以这种方式可靠地工作。你应该原生地分配它:
window.onbeforeunload = function() {
return 'You have not yet saved your work.Do you want to continue? Doing so, may cause loss of your work' ;
}
And in your save method:
在您的保存方法中:
window.onbeforeunload = null;