<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>

CC BY-SA 3.0
2016-08-09T14:54:24.830Z
2016-08-09T15:02:59.847Z
2016-08-09T15:02:59.847Z
Steve Rowling
<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>

CC BY-SA 3.0
2016-08-10T09:15:16.730Z
2016-08-10T09:15:16.730Z
Cole Henley
<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>

CC BY-SA 3.0
2016-08-09T19:02:26.770Z
2016-08-09T19:02:26.770Z
Mats Mikkel Rummelhoff
<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>

CC BY-SA 3.0
2016-08-08T12:24:52.467Z
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?
Luke Pearce
CC BY-SA 3.0
2016-08-08T12:28:52.023Z
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
Cole Henley
<plugin-development><control-panel><ajax><javascript>
Triggering JavaScript from back-end event
# 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 @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  

---

## 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 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:

    Entry saved.


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 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", "");
        }
      })();');
    });
}


---

