这个问题很老了,但为了将来参考,请查看我对原始问题的回答here. 要点是:
Confirmation Dialog
警报在wp admin/js/post中生成。js和wp包括/js/autosave。js。理论上,您应该能够在$中使用以下代码行。成功后功能:
wp.autosave.initialCompareString = wp.autosave.getCompareString();
但这似乎不起作用。相反,您可以替换beforeunload函数,如下所示:
// Replaces wp.autosave.initialCompareString
var ajax_updated = false;
// Supercede the WP beforeunload function
$(window).unbind(\'beforeunload.edit-post\');
$(window).on( \'beforeunload.edit-post\', function() {
var editor = typeof tinymce !== \'undefined\' && tinymce.get(\'content\');
// Use our "ajax_updated" var instead of wp.autosave.initialCompareString
if ( ( editor && !editor.isHidden() && editor.isDirty() ) ||
( wp.autosave && wp.autosave.getCompareString() != ajax_updated) ) {
return postL10n.saveAlert;
}
});
然后在$中将ajax\\u更新为正确的内容。post success功能,因此:
// Mark TinyMCE as saved
if (typeof tinyMCE !== \'undefined\') {
for (id in tinyMCE.editors) {
var editor = tinyMCE.get(id);
editor.isNotDirty = true;
}
}
// Update the saved content for the beforeunload check
ajax_updated = wp.autosave.getCompareString();
Adding an alert message
您还询问了添加警报消息的问题。你可以用很多不同的方法。最简单、最丑陋的方法是使用javascript alert()函数,如下所示:
alert("Post saved");
就我个人而言,我更喜欢
jquery notify library. 包含库后,您将分别将其放置在成功和失败函数中:
$.notify(\'Post saved\',\'success\');
$.notify(\'Something went wrong\',\'error\');