Primefaces Ajax calling Javascript

12,899

Use the PrimeFaces-provided RequestContext API.

First normalize your ajax listener:

<p:ajax event="click" listener="#{cbean.showSaveOverlay}" />

Then add the script to RequestContext#getScriptsToExecute() in the action listener method accordingly:

public void showSaveOverlay() {
    if (...) {
        RequestContext.getCurrentInstance().getScriptsToExecute().add("saveOverlay.show()");
    }
}

If you're not on PrimeFaces 7.0 yet, then use RequestContext#execute() instead:

public void showSaveOverlay() {
    if (...) {
        RequestContext.getCurrentInstance().execute("saveOverlay.show()");
    }
}
Share:
12,899
Landister
Author by

Landister

Updated on October 26, 2022

Comments

  • Landister
    Landister over 1 year

    I was wondering if you could call javascript inside of an ajax statment specificlly I am trying to get the following to work.

            <p:commandLink id="saveButton" value="Save" >
                <p:ajax event="click" actionListener="#{bean.saveButtonPressed()}" />
                <p:ajax event="click" actionListener="if(#{cbean.showSaveOverlay}){saveOverlay.show();}" />
            </p:commandLink>
    

    And showSaveOverly gets set inside saveButtonPressed.

    Any idea how I would do this?