How to define and use a SSJS function inside an XPage

John Dalsgaard • 5. september 2024

Define and use an SSJS function without using a library

I have long been annoyed by not being able to define a server side Javascript (SSJS) function inside an XPage or more often a Custom Control.

You can always add the function to a script library, however, the function may only apply to one control and as such will "pollute" the overall system in a script library.


Then today an idea crossed my mind and I tested it out - and it works...!!


Basically, I tried to add the function to a datacontext variable like this:

    <xp:this.dataContexts>

        <xp:dataContext var="localSSJS">

            <xp:this.value><![CDATA[#{javascript:   

                // Sneak a method in here that can be called from the entire this CC

                var getCellStyle = function(id){

                    var selected = document.getValue(FN_RiscCellId) == id;

                    return 'matrixCell'+getLabel('color5x5Matrix'+id, compositeData.formName) + (selected ? ' circle' : '');

                }

            }]]></xp:this.value>

        </xp:dataContext>

    </xp:this.dataContexts>

And then the code is used later in the Custom Control like this:

    <xp:td id="td11">

        <xp:this.styleClass><![CDATA[#{javascript:getCellStyle('2.5')}]]></xp:this.styleClass>

            :
   
        :
    </xp:td>

And voila - we have a function defined inside one XPage or Custom Control that we can use in several places to avoid duplicating logic - and still don't have to put in a globally visible script library!

Blog

Af John Dalsgaard 23. oktober 2025
What to do if your Notes client on Mac OS starts to "misbehave"?
Af John Dalsgaard 8. september 2025
So I had this idea to test out Nomad Web on a brand-new v.14.5 Domino server as an alternative to the normal Notes client - but it had to fit into the "normal" way I give access to our servers...