Using isDirty() to detect the changes and display a message if leaving page without saving

I am using TinyMCE for my rails app. I am trying to use isDirty() to detect if any changes are made and pop up a message if leaving the page without saving the changes. But the problem I am facing is after adding the javascript as below when I click on the save button in TinyMCE toolbar a message appears as shown in the image. I want to save the changes by clicking on the save button and that message should only appear if I leave the page without saving it. Please help me find the problem

   save_enablewhendirty: false,
   var msg = 'You have unsaved changes.';
            if(isDirty = false){
                isDirty = true;
        window.onbeforeunload = function(){
            if(isDirty = true){
                return msg;