我最近在 TinyMCE 上升级到新版本(4.6.1,不再是最新版本)。我网站的几个区域受到影响,过去常常触发“你想离开这个网站吗?”当编辑了 TinyMCE 框但未提交它所在的表单时发出警告。现在没有触发警告。
我尝试了很多解决方案(自动保存插件、autosave_ask_before_unload 初始化属性、抛出更改事件),但我最接近解决此问题的方法是无论 TinyMCE 区域是否已被编辑都会触发警告。有很多关于如何为 3.x 添加或删除此功能的信息,但对于 4.x 的信息较少。
我需要更改 TinyMCE 字段,以便在您尝试离开页面而不提交表单时触发警告,并且在任何其他情况下都不会触发警告。
最佳答案
建议您使用 IsDirty TinyMCE function
这是一个函数,如果编辑器是否脏了,它会返回一个真/假值。如果用户对内容进行了修改,则编辑器被视为“脏”。
代码:
if (tinyMCE.activeEditor.isDirty())
alert("You must save your contents.");
有一个SO answer这里建议使用 onchange
,但是有一个 open bug with onchange
(it doesn't always fire) .
TinyMCE docs for the onchange callback优先“强烈推荐”IsDirty
,并说明原因:
We strongly recommend users to utilize the isDirty method to determine if the editor contents were changed or not since this is much more exact because it doesn't need undo levels/blurring, etc.
It is important to note that the onchange callback will not fire on each keystroke due to performance considerations. Executing the onchange callback all the time is just a bad design practice and it wastes the users CPU. It's better to check the isDirty state before leaving the page in a document onbeforeunload event. This is how the autosave plugin functions. The isDirty approach is the recommended way of tracking if an editor instance has been modified or not.
关于javascript - 如何在 TinyMCE 编辑时触发页面离开警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44890585/