Running javascript after page load

setup_account ui-mobile-viewport ui-overlay-c

When the page I create such loads:

var location = location.href+"&rndm="+2*Math.random()+" #updatecomment0>*" $("#updatecomment0").load(location, function(){}); 

I have several scripts running on the updatecomment0 div :

 <div id="updatecomment0"> <div id="javascript1">hi</div> <div style="float:right;" class="javascript2">delete</div> </div> 

I don't know how to get these other scripts to run JavaScript after the page loads. Can someone please tell me how with this. Thanks you

+6
javascript jquery pageload
source share
10 answers

Use $(document).ready() .

+12
source share

Use jQuery, you can do it very easily.

 jQuery(document).ready(function(){ alert('Your DOM is ready.Now below this u can run all ur javascript'); }); 
+5
source share

write code inside ready

 jQuery(document).ready(function(){ // write here }); 

: use live or bind

+2
source share

Here is a sample layout for you

 <script type="text/javascript"> $(document).ready(function(){ /// here you can put all the code that you want to run after page load function Javascript1(){ //code here } function Javascript2(){ // code here } $("#btnOK").live('click',function(){ // some codes here Javascript1(); Javascript2(); }); }); </script> <div id="MyDiv"> <input type="button" id="btnOK" value="OK"/> </div> 
+2
source share

If you don't need javascript to do something before the page loads, add your scripts to the bottom hmml document just before the body tag. The page will load faster, and you can do everything you need right in the js file, without document functions.

If the scripts are loaded last, the DOM is guaranteed to be ready.

+1
source share
 $(window).load(function() { // code here }); 
+1
source share

$(document).ready() is all you need.

+1
source share

You can make JavaScript wait a certain time using the setTimeout :

 .setTimeout("name_of_function()",time_in_millis); 
0
source share

Hope this helps too. I had a similar problem and fixed it by calling it immediately after loading the content on the page (for example, after an AJAX request to display the page inside a div):

 (function($) { $(document).ready(function() { // your pretty function call, or generic code }); }(jQuery)); 

Do not forget to call it in the loaded document, but in the function that loads it after it has been loaded.

0
source share

Using vanilla Javascript, this can be done as follows:

 <html> <head> <script type="text/javascript"> // other javascript here function onAfterLoad() { /*...*/ } // other javascript here </script> </head> <body> <!-- HTML content here --> <!-- onAfterLoad event handling --> <div style="display:none;"><iframe onload="onAfterLoad();"></iframe></div> </body> </html> 
0
source share

All Articles