3 Replies Latest reply on Aug 28, 2009 10:25 AM by Mats Reinsby

    Calling javascript functions

    Mats Reinsby Newbie
      Hi all,
      Quite new to SEAM, and I've struggled a bit trying to call a javascript function when body loads on a page. I want to set all <option> tags within a <select> to selected by default. I'm using a <h:selectManyListbox> populated by a <s:selectItems>. It would be great if there's a way to set all elements in the list provided to <s:selectItems> to selected by default, but if that doesn't work then I'll need to call a js function when the page loads (like <body onLoad="setAllSelected()">.

      Can I call js functions via page.xml -> action? Is there a way to set onload on the <ui:define name="body">? Do I have to do it in template.xhtml?

      Any help is much appreciated!!

      -mats-
        • 1. Re: Calling javascript functions
          Arbi Sookazian Master

          I don't think you can call any js functions from pages.xml or components.xml.  AFAIK js only happens in xhtml or JSP files.


          Here's an example I used that disabled some fields when page loaded until user did something to unlock them, etc.


          .xhtml:


          <ui:define name="body">
               
               <script type="text/javascript" src="js/AddHardware.js"/>
               ...
                    <body bgcolor="white" onLoad="return lock();">
          ...
                           </body>
          </ui:define>



          addHardware.js:


          function lock()
          {     
               var selowner = document.getElementById('form1:selowner').value;
               var selmanuf = document.getElementById('form1:selmanufacturer').value;
               var seltype      = document.getElementById('form1:seltype').value;
               var selmodel = document.getElementById('form1:selmodel').value;
               var unitprice = parseFloat(document.getElementById('form1:unitprice').value);                                        
               
               var lockComponents = false;                                   
               if (selowner != "0" && selmanuf != "0" && seltype !="0" && selmodel !="0" && unitprice >= 0)
               {
                    document.getElementById('form1:selowner').disabled = true;
                    document.getElementById('form1:selmanufacturer').disabled = true;
                    document.getElementById('form1:seltype').disabled = true;
                    document.getElementById('form1:selmodel').disabled = true;
                    document.getElementById('form1:unitprice').disabled = true;
                    document.getElementById('form1:displayMessageLabel').innerText ="";          
                    lockComponents = true;
               }
               else 
               {
                    document.getElementById('form1:selowner').focus();
                    freezeBarCodeSerialNoPanel(true);
               }     
               return lockComponents;
          }

          • 2. Re: Calling javascript functions
            Shervin Asgari Master

            Arbi, you should really check out JQuery. You will never write JavaScript that way again :-)