ekremer
ekremer

Reputation: 311

JSF: calling javascript code from Managed Bean not working when forcing page reload

I need to execute a JavaScript code from a JSF Managed Bean's method that is basically to click a button on a facelet (.xhtml file). All this works perfectly by using PrimeFaces' RequestContext.execute("{js here}"), however it no longer works when adding some 'page reload' code lines by using ExternalContext... (please see code below). It seems like the 'page reload' lines prevented the JavaScript code from executing. Any hints is welcome !

public void myMethod() throws IllegalStateException, Exception {

    RequestContext rc = RequestContext.getCurrentInstance();.           

    rc.execute("$('#myButton').click();");

    try {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());

    } catch (IOException e) {
        log.error("Error", e);
    }

Upvotes: 0

Views: 1156

Answers (1)

Rostyslav
Rostyslav

Reputation: 91

RequestContext is for the current request, a redirect is a another request. it can't work.

Try to do something like this:

RequestContext rc = RequestContext.getCurrentInstance();
rc.execute("$('#myButton').click(); window.location = newURL;");

Upvotes: 1

Related Questions