Getting to scope variables in client side javascript (CSJS) on XPages

Bruce Stemplewski picture Bruce Stemplewski · Mar 5, 2012 · Viewed 13.9k times · Source

I am setting a viewScope variable in a server side javascript (SSJS) button.

viewScope.put("branchName",doc.getItemValueString("BranchName"))

How can I access that variable on the client side?

Answer

Declan Lynch picture Declan Lynch · Mar 5, 2012

If you need to access the viewScope variable from a client side script you can use the xp:scriptBlock tag to setup a number of global javascript variables.

<xp:scriptBlock id="scriptBlock1">
<xp:this.value><![CDATA[var myVar = #{javascript:viewScope.get("scopeVar")}]]></xp:this.value>
</xp:scriptBlock>

The main problem with this method is that the inner server side javascript is only computed when that element is initially rendered or when a partial refresh is performed on the element so there is no guarantee that the client side JS variable is set to the correct value.