Server-side JavaScript

Welcome

Login to contribute. Register if you haven't yet.

Support Options

1. Search this site.
2. Make or vote for a Suggestion.
3. Post to Community Support.
3. Contact EditMe Support staff.

Stay Informed

Keep informed of product changes, tutorials and tips via EditMe's Blog and monthly newsletter.

JavaScript API Event Handlers

Back to Developer Support 

EditMe's JavaScript API can call functions in specially named scripted pages during key site events. For example, if you want to track user logins, you might want a function called every time any user logs into your site. EditMe's event model can be used for this purpose.

Event handlers are JavaScript functions with specific names placed into a scripted page called EditMeScriptEvents (or any page name beginning with EditMeScriptEvents). To enable event handling on your site, make a new page called EditMeScriptEvents with the following page attributes:

  • Edit in Plain Text
  • Hide this page from the Index, Recent Changes, and Search
  • MIME Type set to EditMe Scripted Page (text/vnd.editme.xm-js.page)
  • Security set to Adminstrator View/Administrator Edit
See the example below for a template that can be used as a starting point for the EditMeScriptEvents pages.

Currently, not all events are implemented (see the table below to see what's implemented). Contact us if you are working on an application that requires any of the events we haven't gotten to yet, and we'll put a rush on it.

Note that these events do not fire when similar actions are performed by the JavaScript API. The purpose of these events is to run JavaScript code when something happens on the site through EditMe's web interface. 

The following events are either supported or planned. In the table, Function Name is the JavaScript function in the EditMeScriptEvents page that will get called when the event fires. Event Data is the value stored in an implicit "event_data" variable set when the function is called.

Event Description  Function Name  Event Data 
User logs into the site, either via the login form or  "Remember Me" cookie.
editme_event_user_login() String containing the user's name
User completes the Registration form.
editme_event_user_register() String containing the user's name
User is created by an Administrator.
editme_event_user_create() String containing the user's name
User submits the Preferences form or an Administrator updates a user.
editme_event_user_update() String containing the user's name
Administrator deletes a user. editme_event_user_delete() String containing the user's name
New attachment is uploaded.
editme_event_attachment_create() String containing the page name and file name separated by a /. For example: SomePageName/somefile.ext 
Attachment is uploaded or property (deleted, hidden, description) updated.
editme_event_attachment_update() String containing the page name and file name separated by a /. For example: SomePageName/somefile.ext
Attachment is deleted (fully deleted, not marked deleted)
editme_event_attachment_delete() String containing the page name and file name separated by a /. For example: SomePageName/somefile.ext
Planned. editme_event_attachment_view()  
Comment is created.
editme_event_comment_create() ID of comment.
Comment is edited.
editme_event_comment_update() ID of comment.
Comment is deleted.
editme_event_comment_delete() ID of comment.
New page is created.
editme_event_page_create() Page name.
New page version is created.
editme_event_page_update() Page name.
Page is delete (fully deleted, not marked deleted) editme_event_page_delete() Page name.
Planned. editme_event_page_view()  


The following is a template that can be used to create a new EditMeScriptEvents page. Note that if EditMe finds the text "function editme_event_user_login()" in this page content, it will attempt to execute the event handler when a user logs in. It's important that the function not remain in the script commented out, and there must not be a space between the function name and the ().

<div>
<!-- other scripts may be optionally included here, as in:
   <script editme="process 'MySharedFunctions'"/>
-->
<script type="text/vnd.editme.js"><![CDATA[
/** EVENT HANDLERS **/
// this event fires whenever a user logs in
function editme_event_user_login() {
    var userName = event_data;
    // do something with userName here...
}
]]></script>
This page is not intended to be viewed.
</div>

A nice example of some code in an EditMeScriptEvents page and an idea of what it might be used for can be found in the wiki article, How To Add Custom Properties.

Troubleshooting Events

Event programming can be difficult due to the lack of feedback if something goes wrong. Here are some tips to help troubleshoot event scripts.

  • Don't use the 'page' implicit variable in event scripts. It has no meaning in this context and will lead to errors.
  • Test your event scripts by creating a scripted page for testing that processes the event script, sets the event_data variable, and then calls one of the event functions. This will show you whether there are errors in your script.
  • If things aren't working, try stripping your event script down to something extremely simple and get that working, then build from there. For example, the following should always work, and will simply create a test data entry with the name of the page and a time stamp when a page is edited. You can modify this for use with other event handlers.
    function editme_event_page_update() {
      var de=data.get('test.'+event_data);
      if(de==null) {
        de=data.newEntry();
        de.key='test.'+event_data;
      }
      de.value=(new Date()).toString();
      de.save();
    }