Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

how to render <body onload="load()" ..

Ankhaa
2008-12-18
2013-04-25
  • Ankhaa
    Ankhaa
    2008-12-18

    Hi everyone,
    i want to call javascript function on body onload event for particular page, but don't now how :(

    For example only detail.php page should be like this:

    <body onload="load()" >

    Thanks.

     
    • gigamaster
      gigamaster
      2008-12-18

      You can use window event handlers. It's also great to have different scripts working smoothly.
      Basically, an event is when something happens: start loading page, mouse hover a link, click a form button, or when the page finishes loading - all events ^^

      Recent browsers support more events and each element has default event handlers associated with them.
      But you can easily assign a new event handler in a script. There are different ways to do it:

      - Use the Function() contructor

      window.onload=new Function("your code");

      - Assign a function directly to window.onload

      window.onload=function(){
      your code
      }

      - Define the function and assign to the event handler

      function start(){
      your code
      }
      window.onload=start;

      The following sample call the function start() to generate an alert message on page load. 
      and second call the function init() to generate an alert message and select a form element.
      This using window.onload=function(){...} to combine both.

      <html>
      <head>
      <title>Page Title</title>

      <script language="javascript">

      function start(){
      alert("Alert inside start function");
      }

      function init(){
      alert(" Alert inside init function to select input id tf");
      var text_val = document.getElementById("tf");
      text_val.select();
      }

      window.onload=function(){
      start();
      init();
      }

      </script>

      </head>
      <body >

      <form name="form" method="post" action="">
      <input type="text" name="tf" value="XOOPS Cube" id="tf">
      </form>

      </body>

       
    • Ankhaa
      Ankhaa
      2008-12-18

      Thanks Gigamaster for quick reply. I tried and works good :) never mind that so many ways.