最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Confirm navigation" alertbox - Stack Overflow

programmeradmin1浏览0评论

I'm looking for an function that shows a alertbox when the visitor is trying to leave the page when he or she haven't finished the form. I have imported an image to show what I mean. Translation: title = "Confirm navigation", content = "Do you want to leave this page?", button1 = "Leave this page", button2 = "Stay on this page".

Facebook uses this alertbox when you want to go back one page when you have for example not finished an PM to someone.

How can I acplish something like this?

Thanks in advance.

I'm looking for an function that shows a alertbox when the visitor is trying to leave the page when he or she haven't finished the form. I have imported an image to show what I mean. Translation: title = "Confirm navigation", content = "Do you want to leave this page?", button1 = "Leave this page", button2 = "Stay on this page".

Facebook uses this alertbox when you want to go back one page when you have for example not finished an PM to someone.

How can I acplish something like this?

Thanks in advance.

Share Improve this question asked Feb 24, 2012 at 3:07 AirikrAirikr 6,44615 gold badges64 silver badges112 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Use window.onbeforeunload in conjunction with a flag that gets set when a change is made to a form field.

For example:

var changed_flag = 0; // change in an onchange event or whatever, something like the below

document.getElementById('form_field').onchange = function() {
  changed_flag = 1;
};

window.onbeforeunload = function() {
  if ( changed_flag ) {
    return 'You have unsubmitted changes.'
  }
};
发布评论

评论列表(0)

  1. 暂无评论