2

Possible Duplicate:
Javascript that executes after page load

how to call a javascript method as soon as page is loaded.

i have a java script which needs to be called soon after the jsp page is loaded, how to achieve this in javascript.

could any one of you help me pelase.

Regards

Community
  • 1
  • 1
Java Questions
  • 7,813
  • 41
  • 118
  • 176

2 Answers2

3

You can write a code snippet something like this :-

window.onload(function(){
//Your JavaScript here
});

And if using JQuery then

document.ready(function(){
//Your JavaScript Here
});

Or you can have all your JS after all the HTML.

you can even use a function called :--

document.onload(function(){
//Your code Here
});

Last but not the least you could even try out this

 <body onload="YourJSMethod();">
        <!-- Some html content -->
    </body>
Shiv Kumar Ganesh
  • 3,799
  • 10
  • 46
  • 85
  • 1
    With JQuery you can shorten it to $(function () { [your code] }); – Rob Jun 12 '13 at 08:50
  • `window.onload(function(){});` is incorrect. [`addEventListener`](//developer.mozilla.org/en/docs/Web/API/EventTarget/addEventListener) is preferred. – Sebastian Simon Nov 03 '22 at 04:07
2

you can try

<body onload="someMethod();">
    <!-- Some html content -->
</body>

This will call your method as soon as body tag of your page is loaded. Alternatively you can make use of jquery's document ready function.

$(document).ready(function() { 
    // your code 
});
Shades88
  • 7,934
  • 22
  • 88
  • 130
  • i have few methods which loads the value for few combo box through java code, and than set values for loaded combo box once the apge is loaded – Java Questions Sep 17 '12 at 10:30