user2316219
user2316219

Reputation: 334

Trigger SSJS when you open a link

I have portal xpage that has a couple of links. When the user opens a link i want to call some SSJS to log which link the user has clicked.

So far i tried to do it with an onclick event of the link but the problem with that is that window.open is going to be blocked by popup blockers.

Anybody any ideas how to accomplish that?

Upvotes: 0

Views: 825

Answers (2)

user2316219
user2316219

Reputation: 334

I think i got it. It might also get fixed with an asynchronous call but that also works fine.

Create the link and add a client side on click event. Trigger the click of a button with a one second delay. Webpage opens and SSJS fires afterwards. See attached example:

<xp:link escape="true" text="Link" value="Link"> <xp:eventHandler event="onclick" submit="false"> <xp:this.script><![CDATA[id="#{javascript:MenuDocument.getDocument().getUniversalID()}"; setTimeout(function(){$(".Button"+id).click();},1000); ]]></xp:this.script> </xp:eventHandler></xp:link> <xp:button value="log" id="logbutton" style="display:none;"> <xp:this.styleClass><![CDATA[#{javascript:"Button"+MenuDocument.getDocument().getUniversalID()}]]></xp:this.styleClass> <xp:eventHandler event="onclick" submit="true" refreshMode="partial" refreshId="Recent"> <xp:this.action><![CDATA[#{javascript://do whatever you want to do}]]></xp:this.action> </xp:eventHandler> </xp:button>

Upvotes: 0

Serdar Basegmez
Serdar Basegmez

Reputation: 3365

You can run your SSJS as usual. Add a CSJS code into oncomplete property of your event handler within the link...

<xp:link ....>
....
  <xp:eventHandler oncomplete="location.href='http://...url...'">
  ... other stuff ...
  </xp:eventHandler>
</xp:link>

That will make sure your SSJS code runs before the client moves out the page.

UPDATE:

If the target page will be opened on a new tab and window.open is blocked by pop-up blockers, there is one option to run SSJS code via an additional Ajax request to an XAgent. We have implemented such a solution on OpenNTF Collaboration Today site and Niklas Heidloff explains how it works in a NotesIn9 video:

http://notesin9.com/index.php/2012/09/25/notesin9-078-xpages-and-counting-clicks/

In a custom control where we display links to the content, we run a CSJS function logClick() which runs just before the navigation:

<xp:link 
    value="#{javascript:compositeData.link}"
    text="#{javascript:compositeData.title}"
    target="_blank" 
    id="link6">
    <xp:eventHandler event="onclick" submit="false">
        <xp:this.script><![CDATA[
            var baseUrl = "#{javascript:var baseUrl = context.getUrl().toString();baseUrl = baseUrl.substring(0, baseUrl.indexOf('.nsf')+4);baseUrl }";
            var url = baseUrl + "/log.xsp?" + "nid=" + "#{javascript:compositeData.nID}";                       
            logClick(url);      
        ]]></xp:this.script>
    </xp:eventHandler>
</xp:link>

logClick() function is defined in the layout custom control and it simply throw an Ajax request. If you notice, we are preparing the url in log.xsp?nid=ID_OF_ENTRY. log.xsp is an XAgent and it takes the id data and save the 'clicked' event. We have used a caching bean in this example but you can run any SSJS or Java within the XAgent. It does not need to return a data. Here, you might also use an RPC component.

Upvotes: 1

Related Questions