当用户离开窗口而不保存数据时提供自定义消息



谁能给我提供一个自定义警告框说"你即将离开页面,更改将被丢弃"是/否。

我尝试了下面的代码,但它给我默认消息说"您所做的更改可能不会被保存"

这是我的javascript代码

unsaved=true;
window.onbeforeunload = function() { 
    if (unsaved) 
    { 
        var _message = "You currently have unsaved changes!!!nnAre you sure you want to exit without saving.nnChoose ‘Leave this page’ to exit without saving changes.nChoose ‘Stay on this page’ to return to the billing profile."; 
        return _message; 
    } 
}

我也没有表单,但简单的按钮,我不能在表单中包含,点击该按钮也给我警告。这里是小提琴尝试,任何帮助真的很感激,谢谢。我知道之前有关于这个话题的问题,但相信我,现在没有一个是有效的。update有一些方法在以前的StackOverflow示例中解释过,但它们现在在现代浏览器中不起作用

JavaScript中有一个方法叫做window.confirm(): https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm

if (window.confirm("Question"))
  console.log('clicked OK');
else
  console.log('clicked cancel');

试试这个Jquery

$(window).bind('beforeunload', function(){
  return 'Your message here';
});
Javascript

window.onbeforeunload = function(){
  return 'Your message here';
};

最新更新