6

setup_account ui-mobile-viewport ui-overlay-c

When a page i make loads like this:

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

I have multiple 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 make this other JavaScripts run after page load.
Can someone please tell me how to with this.
Thank you

Muhammad Raheel
19.9k7 gold badges69 silver badges106 bronze badges
asked Apr 1, 2011 at 5:33
2
  • 1
    Use the callback function of .load. Commented Apr 1, 2011 at 5:36
  • 1
    Why is everything in boldface? We can read normal font weights too ;) Commented Apr 1, 2011 at 5:50

10 Answers 10

12

Use $(document).ready().

answered Apr 1, 2011 at 5:36
Sign up to request clarification or add additional context in comments.

5 Comments

Will this work even when the content is loaded after the main page?
How are you loading the new content?
Let's suppose the content is loaded using the onClick event of some element. Will this work?
Not exactly sure what your question is. $(document).ready(handlerFunction) will simply execute the handlerFunction when the document is loaded. If you want something to happen to the content loaded using onClick, you can just specify that in the onClick handler.
i use this before $(document).ready() but it not working. the sample layout above, i try with a simple javascript .hide div after .load and it works. thank you
5

Use jQuery, you can do this very easily.

jQuery(document).ready(function(){
 alert('Your DOM is ready.Now below this u can run all ur javascript');
});
answered Apr 1, 2011 at 5:45

2 Comments

yes, i try . i dont know the different between $(document).ready(function(){ with jQuery(document).ready(function(){ . but i will try it again
there is not difference. $ is just a alias of jQuery
3

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>
GMasucci
2,90225 silver badges42 bronze badges
answered Apr 1, 2011 at 6:37

1 Comment

@Benyamin please mark this post as the answer or whichever answer that you think solved your problem. thank you! :D
2

write code inside ready

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

suggestion : use live or bind

answered Apr 1, 2011 at 5:40

1 Comment

yes, i change $(document).ready(function(){ to jQuery(document).ready(function(){
1

Unless you need javascript to do something before the page is loaded, add your scripts to the bottom om the html document, just before the body end tag. The page will load faster, and you can do whatever you need to, right in the js file, without the document ready functions.

If the scripts is the last to load, the DOM is already guaranteed to be "ready".

answered Apr 1, 2011 at 5:38

1 Comment

yes, some of the javascript i need to do something before the page is loaded. i try jQuery(document).ready(function(){ and with .live sample layout and so far is good
1
$(window).load(function() {
 // code here
});
answered Dec 1, 2014 at 1:51

1 Comment

I needed to resize some elements based on the height of another. This worked for me if you're trying something similar.
1

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

answered Dec 1, 2014 at 2:28

Comments

0

You can make JavaScript wait for a specified time using the setTimeout method:

.setTimeout("name_of_function()",time_in_millis);
sth
231k56 gold badges288 silver badges370 bronze badges
answered Apr 1, 2011 at 6:37

Comments

0

I hope this can help you too, I had a similar issue and I fixed it by calling the following just after loading the content in the page (like after an AJAX request for a page to be shown inside a div):

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

Remember to not call this in the document you load, but in the function that load it, after it as been loaded.

answered Mar 19, 2012 at 12:58

Comments

0

Using vanilla Javascript, this can done thusly:

<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>
answered May 10, 2013 at 19:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.