Send a serverside redirect to a new window

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>

4 thoughts to “Send a serverside redirect to a new window”

  1. 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’)

  2. Thank you Mark. I have tried so many things and your solution was the only one that worked. Thanks for sharing!

Comments are closed.