Tuesday, July 31, 2007

Popup windows in JSF

Is is possible to raise a popup window in Java Server Faces (JSF) using Javascript, more or less the same way as in HTML.

This example shows how this can be done with a commandButton.

<h:commandButton value="Press me to bring up a popup window" onclick="window.open('popup.jsp', 'popup_window')" />

This exact method won't work with a commandLink, because this does not offer an onclick option. However the mouseup event can be used instead:

<h:commandLink onclick="window.open('popup.jsp', 'popup_window')">
    Press me to bring up a popup window
</h:commanndLink>

Of course, both commandButton and commandLink items need to be enclosed in a form element. This means that the form will be submitted when the commandButton or commandLink is pressed/clicked. In order to avoid it, we have to return false from within the onclick or mouseup attribute.

<h:commandButton value="Press me to bring up a popup window" onclick="window.open('popup.jsp', 'popup_window'); return false;" />

No comments: