3 Replies Latest reply on Jul 16, 2007 4:28 AM by Cui Michael

    how to set back bean value from javascript?

    Cui Michael Newbie

      Hello,

      I want to send the screen size to the server side during the page loading, how can I do that?

      The following data can be get from javascript.

      window.screen.width;
      window.screen.height;
      window.screen.availWidth;
      window.screen.availHeight;

      One way I want to try to access back bean property from javascript.
      Another way is might be submit hide form field to the sever side.

      Could someone provide me some help about this? Thanks a lot

        • 1. Re: how to set back bean value from javascript?
          Sergey Smirnov Master

          If you want to send this data by pressing ajax button, you can use a4j:actionparam with 'noEscape' attribute set to false.

          Example:

          <a4j:commandButton .......>
           <a4j:actionparam name="width" noEscape="true" value="window.screen.width" />
           <a4j:actionparam name="height" noEscape="true" value="window.screen.height" />
           .....
          </a4j:commandButton>


          You can assign the property of the bean directly with a4j:actionparam using assignTo attribute. Like:
           <a4j:actionparam name="width" noEscape="true" value="window.screen.width" assignTo="#{screenbean.width}" />


          In general, a4j:jsFunction allows to send any data from javascript. a4j:actionParam are also used there similar to the example above.

          • 2. Re: how to set back bean value from javascript?
            Cui Michael Newbie

            Hi, SergeySmirnov,

            Many thanks for your quick reply!

            One more question: I want to initialize the Screen Bean during the page loading time and it will only be initialized once. How can I realize this case? I try to use the following code but it didn't work.

            <body onload="javascript: deleteUserScreenSize();">
            <a4j:jsFunction name="deleteUserScreenSize"
             action="#{screenBean.deleteUserScreenSize}"
             reRender="form1">
             <a4j:actionparam name="width" noEscape="true" value="window.screen.width" assignTo="#{screenBean.width}" />
             <a4j:actionparam name="height" noEscape="true" value="window.screen.height" assignTo="#{screenBean.height}" />
            </a4j:jsFunction>
            

            -hefeng

            • 3. Re: how to set back bean value from javascript?
              Cui Michael Newbie

              After looking at the sample of a4j:jsFunction, I found the way implement my case. The following is the code.

              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml"
               xmlns:ui="http://java.sun.com/jsf/facelets"
               xmlns:h="http://java.sun.com/jsf/html"
               xmlns:f="http://java.sun.com/jsf/core"
               xmlns:t="http://myfaces.apache.org/tomahawk"
               xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
               xmlns:rich="http://richfaces.ajax4jsf.org/rich">
               <head>
               <title>Detect screen size</title>
              
               </head>
              <body onload="deleteUserScreenSize()">
              
              
              <f:view>
              <h:form id="form1" >
              <a4j:jsFunction name="deleteUserScreenSize"
               action="#{sbean.deleteUserScreenSize}"
               reRender="form1">
               <a4j:actionparam name="width" noEscape="true" value="window.screen.width" assignTo="#{sbean.width}" />
               <a4j:actionparam name="height" noEscape="true" value="window.screen.height" assignTo="#{sbean.height}" />
              </a4j:jsFunction>
              </h:form>
              </f:view>
              </body>
              </html>


              Cheers