javascript - How to display onbeforeunload dialog when appropriate? - Stack Overflow

I've got an editor in javascript on my webpage and I would like to ask user if heshe wants to lea

I've got an editor in javascript on my webpage and I would like to ask user if he/she wants to leave the page even if there are unsaved changes.

I know I can add custom message to the "onbeforeunload dialog" this way:

window.onbeforeunload = function() {
  return 'You have unsaved changes!';
}

(Source) but I want to display the dialog only where there really are some unsaved changes. How to do that?

Thanks!

I've got an editor in javascript on my webpage and I would like to ask user if he/she wants to leave the page even if there are unsaved changes.

I know I can add custom message to the "onbeforeunload dialog" this way:

window.onbeforeunload = function() {
  return 'You have unsaved changes!';
}

(Source) but I want to display the dialog only where there really are some unsaved changes. How to do that?

Thanks!

Share Improve this question edited May 23, 2017 at 11:45 CommunityBot 11 silver badge asked Apr 18, 2010 at 19:59 MartyIXMartyIX 28.7k32 gold badges139 silver badges217 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You can do something like this:

var unsavedChanges = false;
window.onbeforeunload = function() {
  if (unsavedChanges) return 'You have unsaved changes!';
}

function makeSomeChange() {
  // do some changes....
  unsavedChanges = true;
}

You can make sure to change unsavedChanges in the 'change' event handlers.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399548a4572316.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信