xpages

Xpages button onclick event runs on document load


The text 'button 2 pressed' is added to log.nsf on document load (i.e. when copy/paste the URL into the browser address bar). It also runs when I click it too which is the desired behaviour. Can anyone tell me what's going on? There's nothing in the HCL documentation for the onclick event or the event handler that describes this behaviour and/or what to do to stop it occurring.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xc="http://www.ibm.com/xsp/custom">
    <xp:this.data>
        <xp:dominoDocument formName="fParent" var="document1" />
    </xp:this.data> 
    <xp:button value="Click me" id="button2" type="submit"
        refreshMode="complete">
        <xp:this.onclick><![CDATA[#{javascript:print("button 2 pressed");}
        ]]></xp:this.onclick>
    </xp:button>
</xp:view>

Solution

  • onclick is a client side event and since your client side event contains server side code, the code runs when the page is loaded.

    If you want to only run the server side code when the button is clicked, you should use the action event of the eventHandler.

    So your button should look like this instead:

    <xp:button value="Click me" id="button2">
        <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
            <xp:this.action><![CDATA[#{javascript:print("button 2 pressed");}]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>