Search code examples
javascriptajaxrichfacesajax4jsf

open a new window in a4j:commandButton


Using A4J, Richfaces in a web application, I need to open a new browser window when the user clicks on the <a4j:commandButton>.

I think I will have to use window.open(URL, ...). Where should I put it?

My <a4j:commandButton> looks like this:

<a4j:commandButton id="elementDetailsButton"
    onclick="if (!confirm('Are you sure? Unsaved data will be lost')) { return false; }"
    action="#{myBean.elementDetailsAction}"
    value="Element Details">
    <a4j:actionparam name="elementDetailsString"
        value="getElementDetails()"
        assignTo="#{myBean.elementDetails}" noEscape="true">
    </a4j:actionparam>
</a4j:commandButton>

Solution

  • You can confirm the window.open and not the return false

    <a4j:commandButton id="elementDetailsButton"
        onclick="if (confirm('Are you sure? Unsaved data will be lost')) { window.open(URL, ...) } else { return false }" (...) />
    

    "Else" is optional, maybe not necessary.

    Or you can change the form target.. I dont remember very well if its the correct syntax...

    <a4j:commandButton id="elementDetailsButton" onclick="this.form.taget='_blank'" (...) />
    

    ...or something like that.

    Changing the form target will give you a nice problem. The rest of you application will target the new window.. To solve this problem, I made a <h:commandLink/> to close the window (modalPanel) and reset the form target.

    I was using this (the target trick) to open .pdf reports inside a <rich:modalPanel/> using a <iframe/>.

    But I'm not sure if change the form target will be useful for your problem..