Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-08T18:55:49.810Z
Answers.LastActivityDate: 2016-01-08T18:55:49.810Z
Answers.UserDisplayName: Ben Croker
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-11-12T06:22:08.097Z
Comments.Text: Should be able to add 'data-confirm-unload' to your form tag and Craft should pick it up?
Comments.UserDisplayName: Luke Holder
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-11-12T06:24:10.153Z
Comments.Text: 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
Comments.UserDisplayName: Luke Holder
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-11-12T06:27:10.390Z
Comments.Text: 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?
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-11-16T04:30:44.637Z
Comments.Text: you couldn't just see if the field is on an element with an `->id` ?
Comments.UserDisplayName: Luke Holder
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-11-16T05:09:14.957Z
Comments.Text: @LukeHolder: Interesting idea! However, I also need to know if the entry has _changed_... not just if it's been saved the first time.
Comments.UserDisplayName: Lindsey D
Tags: <plugin-development><javascript><saving>
Title: JS - Determine that an entry is unsaved?
ThreadText: # 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 @username_1. 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**: @username_1: 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](https://github.com/pixelandtonic/Craft-Release/blob/master/app/resources/js/cp.js#L196).
  
  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;
  }
  
  ```
  
  
  ---
  
  