<p>Craft checks if a form has changed using javascript in the cp.js file, see <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/resources/js/cp.js#L196" rel="nofollow">line 196</a>.</p>

<p>You could replicate this quite easily yourself though, here is a stripped down example:</p>

<pre><code>var initialFormValues = $('form[data-confirm-unload]').serialize();

function checkFormChanged() {
    if ($('form[data-confirm-unload]').serialize() != initialFormValues) {
        return true;
    }

    return false;
}
</code></pre>

CC BY-SA 3.0
2016-01-08T18:55:49.810Z
2016-01-08T18:55:49.810Z
Ben Croker
<p>I've created a custom fieldtype... which is basically a button that triggers an AJAX action. When I add this field to my section, I can click the button while I'm editing an entry to perform an action.</p>

<p>Here's the thing... <strong>How can I tell if the entry needs to be saved?</strong> Clearly Craft is aware of whether an entry is unsaved or not, because it'll warn you before navigating away from the page. Is it possible for my button to tap into that same behavior?</p>

<p><strong>MORE INFO:</strong></p>

<p>This button does not take people away from the page, so I'm not sure if <code>data-confirm-unload</code> really comes into play here. If the entry is unsaved, the button will be replaced with a message saying, "you must first save the entry".</p>

CC BY-SA 3.0
2015-11-12T06:22:08.097Z
Should be able to add 'data-confirm-unload' to your form tag and Craft should pick it up?
Luke Holder
CC BY-SA 3.0
2015-11-12T06:24:10.153Z
is gets used around here: https://github.com/pixelandtonic/Craft-Release/blob/bf0eb8cab9ba5f2cca4789053dee1e925271978d/app/resources/js/cp.js#L144 might be able to see how it uses window's `beforeunload` js event
Luke Holder
CC BY-SA 3.0
2015-11-12T06:27:10.390Z
Thanks @LukeHolder. I'm not really concerned with folks navigating away... I want the button to _only appear if the entry is saved_ (and show a "must save" message otherwise). It's a standard entry page, so I believe the form already contains a `data-confirm-unload` attribute. Is that still the right direction?
Lindsey D
CC BY-SA 3.0
2015-11-16T04:30:44.637Z
you couldn't just see if the field is on an element with an `->id` ?
Luke Holder
CC BY-SA 3.0
2015-11-16T05:09:14.957Z
@LukeHolder: Interesting idea! However, I also need to know if the entry has _changed_... not just if it's been saved the first time.
Lindsey D
<plugin-development><javascript><saving>
JS - Determine that an entry is unsaved?
# JS - Determine that an entry is unsaved?

(Asked by: username_0 on 2015-11-12)

I've created a custom fieldtype... which is basically a button that triggers an AJAX action. When I add this field to my section, I can click the button while I'm editing an entry to perform an action.

Here's the thing... **How can I tell if the entry needs to be saved?** Clearly Craft is aware of whether an entry is unsaved or not, because it'll warn you before navigating away from the page. Is it possible for my button to tap into that same behavior?

**MORE INFO:**

This button does not take people away from the page, so I'm not sure if `data-confirm-unload` really comes into play here. If the entry is unsaved, the button will be replaced with a message saying, "you must first save the entry".

**Tags:** plugin-development, javascript, saving
> **Comments:**
> **username_1**: Should be able to add 'data-confirm-unload' to your form tag and Craft should pick it up?  
> **username_1**: is gets used around here: https://github.com/pixelandtonic/Craft-Release/blob/bf0eb8cab9ba5f2cca4789053dee1e925271978d/app/resources/js/cp.js#L144 might be able to see how it uses window's `beforeunload` js event  
> **username_0**: Thanks @LukeHolder. I'm not really concerned with folks navigating away... I want the button to _only appear if the entry is saved_ (and show a "must save" message otherwise). It's a standard entry page, so I believe the form already contains a `data-confirm-unload` attribute. Is that still the right direction?  
> **username_1**: you couldn't just see if the field is on an element with an `->id` ?  
> **username_0**: @LukeHolder: Interesting idea! However, I also need to know if the entry has _changed_... not just if it's been saved the first time.  

---

## 1 answers

### Answer by username_2 on 2016-01-08. Score: 3

Craft checks if a form has changed using javascript in the cp.js file, see line 196.
You could replicate this quite easily yourself though, here is a stripped down example:
var initialFormValues = $('form[data-confirm-unload]').serialize();

function checkFormChanged() {
    if ($('form[data-confirm-unload]').serialize() != initialFormValues) {
        return true;
    }

    return false;
}


---

