myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Myfaces Wiki] Update of "JavascriptOpenNewWindow" by Rene Grob
Date Mon, 26 Feb 2007 20:10:38 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The following page has been changed by Rene Grob:
http://wiki.apache.org/myfaces/JavascriptOpenNewWindow

------------------------------------------------------------------------------
   * The appearance of the new window cannot be controlled (e.g. hiding of the location bar)
  If no model update is needed then setting the attribute "immediate" to "true" can be used
to work around the validation problems.
  
+ Note: This is the only way to open a new browser window without using Javascript.
+ 
- == Using HTML and Javascript ==
+ == Using an OutputLink and Javascript ==
  {{{
- <a href="#" onclick="window.open('popup.faces', 'popupWindowName', 'dependent=yes, menubar=no,
toolbar=no');">open popup</a>
+ <h:outputLink onclick="window.open('popup.faces', 'popupWindowName', 'dependent=yes,
menubar=no, toolbar=no'); return false;" value="#">
+ 	<h:outputText value="open popup" />
+ </h:outputLink>
  }}}
  With this solution we got control over the appearance of the new browser window. And since
there is no postback, there is no validation at all.
  This is the easiest way to open a new browser window when no model update is needed and
no action has to be executed.
  
  == Advances techniques ==
- In order to implement a proper action handling we need to move the decision wether to open
a new window to the action listener.
+ In order to implement a proper action handling we need to move the decision whether to open
a new window to the action listener.
  {{{
  <h:commandLink actionListener="#{bean.openPopupClicked}" value="open popup" />
  }}}
@@ -52, +56 @@

      ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
      String actionUrl = viewHandler.getActionURL(facesContext, viewId);
      
-     String javaScriptText = "window.open('"+actionUrl+"', 'popupWindow', 'dependent=yes,
menubar=no, toolbar=no');";
+     String javaScriptText = "window.open('"+actionUrl+"', 'popupWindow', 'dependent=yes,
menubar=no, toolbar=no'); return false;";
      
      // Add the Javascript to the rendered page's header for immediate execution
      AddResource addResource = AddResourceFactory.getInstance(facesContext);
@@ -61, +65 @@

  }}}
  
  === Closing the popup window ===
- If the close button has a "cancel" semantic, we can use a simple DHTML script:
+ If the close button has a "cancel" semantic, we can use a simple Output''''''Link:
  {{{
- <a href="#" onclick="window.close();">open popup</a>
+ <h:outputLink onclick="window.close(); return false;" value="#">
+ 	<h:outputText value="cancel" />
+ </h:outputLink>
  }}}
  The window will simply close. The server will not notice anything.
  
  === Submit form and close the popup window ===
  For a proper action handling which goes through the whole request lifecycle we need to use
an action listener:
  {{{
- <h:commandLink actionListener="#{popupBean.closeWindow}" value="submit and close" />
+ <h:commandLink actionListener="#{popupBean.closeWindowClicked}" value="submit and close"
/>
  }}}
  
  {{{
  public void closeWindowClicked(ActionEvent event) {
      FacesContext facesContext = FacesContext.getCurrentInstance();
  
-     String javaScriptText = "window.close();";
+     String javaScriptText = "window.close(); return false;";
      
      // Add the Javascript to the rendered page's header for immediate execution
      AddResource addResource = AddResourceFactory.getInstance(facesContext);

Mime
View raw message