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

1

2 Answers 2

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>
2
  • 1
    With JQuery you can shorten it to $(function () { [your code] });
    – Rob
    Commented Jun 12, 2013 at 8:50
  • window.onload(function(){}); is incorrect. addEventListener is preferred. Commented Nov 3, 2022 at 4: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 
});
1
  • 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 Commented Sep 17, 2012 at 10:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.