Redirecting a user on an XPage server side can be done by calling one of the context.redirectTo() methods or by using the facesContext.getExternalContext().redirect() method. But what if you want to open that page in a new window/ tab?
The only way to open a link (AFAIK) in a new window/tab is by making a clientside JavaScript window.open(”) call. There are a couple of ways to send that call to the browser (using the onComplete event or a conditionally rendered outputScript control), but the XPages engine also has a little known method called view.postScript(“”). Anything you add there is send to the browser upon completing the eventhandler as clientside script.
Using that, opening the result of an eventhandler in a new window/ tab can then be accomplished by writing:
<xp:link escape="true" text="Start" id="link2"> <xp:eventHandler event="onclick" submit="true" refreshMode="partial" refreshId="link2"> <xp:this.action><![CDATA[#{javascript: //execute server side code here to determine a target url var target = "http://www.nu.nl"; view.postScript("window.open('" + target + "')");}]]> </xp:this.action> </xp:eventHandler> </xp:link>
Good to know. Thanks Mark!
Cool – thanks Mark – and this has a LOT more possibilities than just redirecting the user. It is interesting to see how this works. The partialRefresh inserts the following into the code adjacent to the link. It reattaches the link so that it will work again and adds the window.open. This could be used as a serverside generated call back to a function post partial refresh – love it 🙂
XSP.addOnLoad(function() {
XSP.attachPartial(“view:_id1:_id2”, “view:_id1:link2”, null, “onclick”, function(){}, 2, “view:_id1:link2”);
});
window.open(‘http://www.cnn.com’)
Nice finding! This solves some nasty ‘ render outputscript when requestscope is set ‘ constructions 😛
Thank you Mark. I have tried so many things and your solution was the only one that worked. Thanks for sharing!