Martin
Martin

Reputation: 31

call ssjs within javascript-onbeforeunload

I'm not very used to xpages and webprogramming and I'm stuck here with a problem for a few days now.

Is there any way to call a server side java script within the javascript-onbeforeunload-event? What I have to do is to delete a notesdocument in a notesview when the user leaves the page.

f.eg:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
xmlns:xe="http://www.ibm.com/xsp/coreex">
<script language="JavaScript">
    window.onbeforeunload = callSSJS;
    function callSSJS(){
      here I'd like to call a ssjs - f.eg. 
         var vw:NotesView = database.getview("anyview");
         var doc:NotesDocument = vw.getDocumentByKey("123");
         doc.remove(true);
     }
</script>
</xp:view>

Upvotes: 2

Views: 1644

Answers (1)

Sven Hasselbach
Sven Hasselbach

Reputation: 10485

This can be realized with the execOnServer method from Jeremy Hodge: http://xpagesblog.com/XPagesHome.nsf/Entry.xsp?documentId=88065536729EA065852578CB0066ADEC

First create a event in your XPage:

<xp:eventHandler event="onunload" id="onUnload" submit="false">
    <xp:this.action>
        <xp:executeScript>
            <xp:this.script><![CDATA[#{javascript:
                var vw:NotesView = database.getview("anyview");
                var doc:NotesDocument = vw.getDocumentByKey("123");
                doc.remove(true);
            }]]></xp:this.script>
        </xp:executeScript>
    </xp:this.action>
</xp:eventHandler>

Then add the CSJS output script block to your XPage:

<xp:scriptBlock id="scriptBlockOnUnload">
    <xp:this.value>
    <![CDATA[

        var executeOnServer = function () {

            // must supply event handler id or we're outta here....
            if (!arguments[0])
                return false;

            // the ID of the event handler we want to execute
            var functionName = arguments[0];

            // OPTIONAL - The Client Side ID that you want to partial refresh after executing the event handler
            var refreshId = (arguments[1]) ? arguments[1] : "@none";
            var form = (arguments[1]) ? XSP.findForm(arguments[1]) : dojo.query('form')[0];

            // OPTIONAL - Options object contianing onStart, onComplete and onError functions for the call to the
            // handler and subsequent partial refresh
            var options = (arguments[2]) ? arguments[2] : {};

            // Set the ID in $$xspsubmitid of the event handler to execute
            dojo.query('[name="$$xspsubmitid"]')[0].value = form.id + ':' + functionName;
            XSP._partialRefresh("post", form, refreshId, options);

        }

        window.onbeforeunload = function(){

                        if( ! dojo._xhr )
                           dojo._xhr = dojo.xhr;

                        dojo.xhr = function ( args, ioArgs, addArgs ){
                           ioArgs["sync"] = true;
                   ioArgs["failOk"] = true;
                           return dojo._xhr( args, ioArgs, addArgs );
                        }
                        executeOnServer('onUnload');
                    };
    ]]>
    </xp:this.value>
</xp:scriptBlock>

The last line is the hook for the unload method which calls the server side event to delete the document.

EDIT:

Added a dojo ioArgs for a sync call and a better failure handling.

Upvotes: 3

Related Questions