检查onbeforeunload中的ace编辑器,查看是否进行了更改

checking the ace editor in the onbeforeunload to see if changes were made

本文关键字:是否 中的 onbeforeunload ace 编辑器 检查      更新时间:2023-09-26

我正在尝试使用ace编辑器中的$(window).on('beforeunload', function(){});editor.session.getUndoManager().isClean();来检查用户是否对文档进行了更改,但没有单击提交按钮,但由于某些原因它不起作用。下面是代码:

editor.on('change', function(Isclean ) {
        var Isclean = editor.session.getUndoManager().isClean();
    });
var submitting = false;
$(window).on('beforeunload', function(){
    if (submitting == false && Isclean == false) {
            return "You have made some changes in the editor.";
    };
});

但无论出于何种原因,Isclean总是返回布尔值true。

代码中有几个问题。在"change"事件处理程序中,函数参数和局部变量都被称为Isclean

其次,Isclean变量对"beforeunload"事件处理程序不可见。

为了完成这项任务,我做了:

    $(window).bind("load", function() {
    editor.session.getUndoManager().markClean();
});

我在编辑器中预加载了一些Json,所以除非我调用editor.session.getUndoManager().markClean();作为最后加载的东西(它必须是最后加载的),否则它总是布尔值为false。