onchange_callback

This option should contain a function name to be executed each time content is modified by TinyMCE. This function is called each time an undo level is added to TinyMCE. The format of this function is: onchange(inst), where inst is the editor instance object reference.

Example of usage of the onchange_callback option

function myCustomOnChangeHandler(inst) {
alert("Some one modified something");
alert("The HTML is now:" + inst.getBody().innerHTML);
}

tinyMCE.init({
...
onchange_callback : "myCustomOnChangeHandler"
});

Note:

This function is called once the user "blurs" the area (in the same way a textarea or input field is blurred upon exiting that field) and it will check if a change has occurred. It will also fire when a new undo level is added to the queue. Undo levels are added when the user types text and then moves the cursor, performs an action like pressing the bold button while having text selected, or pressing return. There are many ways undo levels get added to the editor.

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.

User Image
  • 2011-06-20 22:51:53

omniasoft

Just a follow up... if I'm in the editor and add a bunch of letters without hitting enter, and then go up to the "X" to close the window the dirty status is not set to true.

If I'm in the editor and just hit enter somewhere, then go up to the "X" to close the window, the dirty status is then set to true.

TinyMCE should register the control as being dirty when someone inputs just letters and then goes up to the "X" to close the window.

User Image
  • 2011-06-20 22:46:10

omniasoft

The problem is that in some browsers if we actually change the contents of the editor and then go up to the "X" button to close the window without blurring the editor first, TinyMCE doesn't update the dirty state of the editor.