Conditionally invoking JavaScript with navigation

UI Components for JSF
Post Reply
csharp
Posts: 17
Joined: 27 Aug 2010, 02:39

10 Apr 2013, 19:05

Hello,

I am having some problems getting RequestContext to work in the way required. Basically I have a login page, and if the login is unsuccessful, the page is redisplayed with error messages. This works correctly. If the login is successful, it navigates to one of three required pages. This is also successful.

However, I want a popup window to be displaying giving the user some information if the login is successful, but not if it fails.

I have the following fragment of code:

Code: Select all

public String login() {

    // Other code

    RequestContext context = RequestContext.getCurrentInstance(); 
    context.execute("openPopup(750,650,'popups/gotoOption.f')");
    return "option" + optNum;
}
where on success the last part is executed which opens a popup window and returns "option?" where ? = 1, 2, or 3. If the login fails, this part of the code is never executed, and instead "login" is returned, where "login.xhtml" is the login page, and various error messages are displayed.

My xhtml file has the following line of code:

Code: Select all

<p:commandButton id="login" value="Login" update="out" action="#{login.login}" styleClass="button"/>
which fires the action, and my JavaScript code is:

Code: Select all

var winRef=null;
var winFeatures=",toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=yes,menubar=no";

function openPopup(w,h,f) {
    close popup();
    var x = (screen.width-w)/2;
    var y = (screen.height-h)/2;	
    var settings="width="+w+",height="+h+",left="+x+",top="+y+winFeatures;
    var ext = f.indexOf(".f");
    if (ext < 0) winRef = window.open(f+".html",f,settings);
    else           winRef = window.open(f+"aces",f.substring(0,f.length-2),settings);
}
function closePopup() {
    if (winRef == undefined) return;
    if (!winRef.closed) winRef.close();
}
which in turn opens the the window containing the following code:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
  <h:head>
  </h:head>
  <h:body>
    <h:form id="loginPopup" prependId="false">
      <h:commandButton value="OK" onclick="self.close()"/>
    </h:form>
  </h:body>
At the moment all that is supposed to happen is that the window opens and just displays a button, which when clicked is supposed to close and navigate the user to the selected page. The idea is that text will eventually be displayed in the window giving the user some information, which will also include some JavaScript.

At the moment the window is not opened if my login action navigates to the required page on success. If I replace:

Code: Select all

return "option" + optNum;

//by

return "";

// then add the method

public String gotoOption() {
    System.out.println("Going to: option" + optNum);
    return "option" + optNum;
}
and change my commandButton in the popup window so that it is now:

Code: Select all

<h:commandButton value="OK" action="#{login.gotoOption}" onclick="self.close()"/>
this still does not work properly.

My JavaScript and the popup window work correctly, as I have used the code elsewhere, and the RequestContext also works and invokes the JavaScript, but only if I do not try and navigate away from the page being displayed. If I do, it does not work. Also the popup window will not cause navigation to take place.

Does anybody know how to invoke conditionally a popup window, and when it is displayed, on clicking "OK" the window is closed and the main window then navigates to the required page? I also looked at <p:dialog/>, but that has not helped so far.

Any suggestions would be most welcomed.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 77 guests