tags:

views:

97

answers:

3

How can I call a JavaScript function on page load without making

 <body onload='callFunction()'>

i want to check when loading the page what is the purpose of using that page, if for addition then ok , if for edition , there will be data come from server to be displayed in that page, so i want to check the request each time page is loaded , since i don't have body tag in my page cz i use jspf (JSP Fragment) . NOTE : i don't know jQuerry , is there's another way?

+3  A: 
window.onload = yourfunction;

Or with jQuery if you want:

$(function(){
   yourfunction;
});

If you want to call more than one function on page load, take a look at this article for more information:

Sarfraz
This is not correct, the function will execute and assign whatever it returns to onload. The () should not be there.
epascarello
If there are other functions that are listening for the onload event this will blow them away. Better to add an event listener rather than assign an event handler directly. Even in this case you would assign it as `window.onload = yourfunction' without the function closure, not the way you have it. Your way seeks to execute yourfunction() at the time of the assignment.
Robusto
that will assign the return value of the function to window.onload, which will not work, unless the return value is a function.
I.devries
@epascarello: Good catch, fixed, you see a habit when writing function names. Thanks
Sarfraz
window.onload = yourfunction; The problem with this way is that it dosen't accept fucntion parameter !!
Alaa
A: 

Your original question was unclear, assuming Kevin's edit/interpretation is correct then this first option doesn't apply

The typical options is using the onload event:

<body onload="javascript:SomeFunction()">
....

You can also place your javascript at the very end of the body; it won't start executing until the doc is complete.

<body>
  ...
  <script type="text/javascript">
    SomeFunction();
  </script>
</body>

And, another options, is to consider using a JS framework which intrinsically does this:

// jQuery
$(document).ready( function () {
  SomeFunction();
}
STW
A: 

If you want the onload method to take parameters, you can do something similar to this:

window.onload = function()
                {
                   yourFunction(param1, param2);
                };

This binds onload to an anonymous function, that when invoked, will run your desired function, with whatever parameters you give it. And, of course, you can run more than one function from inside the anonymous function.

Matt S
And now when I'm dynamically including a server-generated page and need DOM-readiness, I need to work through this minefield. If only someone would have encouraged properly library user earlier.
Stefan Kendall
I didn't say it was a good idea. Though the lead dev where I work has a strong case of Not Invented Here syndrome, and I haven't been able to convince him to use jquery, outside of a few pages.
Matt S
Then perhaps you should switch jobs. If the right tool for the job isn't the tool you're using, why beat your head against the wall constantly battling the incompetent?
Stefan Kendall