Answers.Body: <p>When an entry is saved, an <code>Entry saved.</code> flash message is set and displayed by Craft. You could use the <a href="https://github.com/lindseydiloreto/craft-cpjs">Control Panel JS plugin</a> to write a script to detect this flash message and perform your desired action.</p>
  
  <p>The flash message is set in a <code>div</code> with an <code>id="notifications"</code>. When there is no notification present, this <code>div</code> is empty. When there is a notification present, the <code>div</code> contains another <code>div</code> like this:</p>
  
  <pre><code>&lt;div id="notifications"&gt;
      &lt;div class="notification notice" style="opacity: 0; display: none;"&gt;Entry saved.&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>If you test for the presence of this inner <code>div</code> and also the specific notification content (i.e. <code>Entry saved.</code>) you should be able to trigger the actions you need to.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-09T14:54:24.830Z
Answers.LastActivityDate: 2016-08-09T15:02:59.847Z
Answers.LastEditDate: 2016-08-09T15:02:59.847Z
Answers.UserDisplayName: Steve Rowling
Answers.Body: <p>Thanks both for answers - realised that was probably going about things the wrong way so have tried a different approach. Using a plugin to store a timestamp of last updated article. This is saved as a cookie in front-end and on page load the value in the users session is compared to the value from the plugin. If these don't match then the cookie is reset.</p>
  
  <p>Not really worked on this kind of plugin before so some really useful feedback and suggestions, thanks!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-10T09:15:16.730Z
Answers.LastActivityDate: 2016-08-10T09:15:16.730Z
Answers.UserDisplayName: Cole Henley
Answers.Body: <p>Since you're building your own plugin, you can make use of the UserSessionService's <a href="https://craftcms.com/classreference/services/UserSessionService#addJsFlash-detail" rel="nofollow">addJsFlash() method</a> to queue up some JavaScript that will be injected and executed upon the next request after an entry is saved:</p>
  
  <p>(In your plugin's main class)</p>
  
  <pre><code>public function init()
  {
      craft()-&gt;on('entries.saveEntry', function (Event $event) {
        craft()-&gt;userSession-&gt;addJsFlash('(function () {
          if (window.Craft) {
              Craft.setLocalStorage("elementindex.Entry", "");
          }
        })();');
      });
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-09T19:02:26.770Z
Answers.LastActivityDate: 2016-08-09T19:02:26.770Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I'm building a plugin and want to trigger some JavaScript on an event being fired (eg an entry being saved). What is the best way to achieve this? Coming up short looking through the documentation and Stack Overflow.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-08T12:24:52.467Z
Comments.Text: What is it you're trying to achieve using Javascript? Where are you saving the entry? Is this trigger for the back end/front end?
Comments.UserDisplayName: Luke Pearce
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-08T12:28:52.023Z
Comments.Text: thanks for reply @LukePearce - trying to clear `localStorage` on an entry save but needs to be tied to specific/defined sections (hence focus on using a plugin). 
  
  This is for the back-end/control panel
Comments.UserDisplayName: Cole Henley
Tags: <plugin-development><control-panel><ajax><javascript>
Title: Triggering JavaScript from back-end event
ThreadText: # Triggering JavaScript from back-end event
  
  (Asked by: username_0 on 2016-08-08)
  
  I'm building a plugin and want to trigger some JavaScript on an event being fired (eg an entry being saved). What is the best way to achieve this? Coming up short looking through the documentation and Stack Overflow.
  
  **Tags:** plugin-development, control-panel, ajax, javascript
  
  > **Comments:**
  > **username_1**: What is it you're trying to achieve using Javascript? Where are you saving the entry? Is this trigger for the back end/front end?  
  > **username_0**: thanks for reply @username_1 - trying to clear `localStorage` on an entry save but needs to be tied to specific/defined sections (hence focus on using a plugin).   This is for the back-end/control panel  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2016-08-09. Score: 4
  
  When an entry is saved, an `Entry saved.` flash message is set and displayed by Craft. You could use the [Control Panel JS plugin](https://github.com/lindseydiloreto/craft-cpjs) to write a script to detect this flash message and perform your desired action.
  
  The flash message is set in a `div` with an `id="notifications"`. When there is no notification present, this `div` is empty. When there is a notification present, the `div` contains another `div` like this:
  
  ```
  <div id="notifications">
      <div class="notification notice" style="opacity: 0; display: none;">Entry saved.</div>
  </div>
  
  ```
  
  If you test for the presence of this inner `div` and also the specific notification content (i.e. `Entry saved.`) you should be able to trigger the actions you need to.
  
  
  ---
  
  ### Answer by username_0 on 2016-08-10. Score: 2
  
  Thanks both for answers - realised that was probably going about things the wrong way so have tried a different approach. Using a plugin to store a timestamp of last updated article. This is saved as a cookie in front-end and on page load the value in the users session is compared to the value from the plugin. If these don't match then the cookie is reset.
  
  Not really worked on this kind of plugin before so some really useful feedback and suggestions, thanks!
  
  
  ---
  
  ### Answer by username_3 on 2016-08-09. Score: 2
  
  Since you're building your own plugin, you can make use of the UserSessionService's [addJsFlash() method](https://craftcms.com/classreference/services/UserSessionService#addJsFlash-detail) to queue up some JavaScript that will be injected and executed upon the next request after an entry is saved:
  
  (In your plugin's main class)
  
  ```
  public function init()
  {
      craft()->on('entries.saveEntry', function (Event $event) {
        craft()->userSession->addJsFlash('(function () {
          if (window.Craft) {
              Craft.setLocalStorage("elementindex.Entry", "");
          }
        })();');
      });
  }
  
  ```
  
  
  ---
  
  