GeeCON Prague 2014*
The moose likes HTML, CSS and JavaScript and the fly likes jQuery load with Callback versus Document Ready Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "jQuery load with Callback versus Document Ready Method" Watch "jQuery load with Callback versus Document Ready Method" New topic
Author

jQuery load with Callback versus Document Ready Method

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1462
    
    1
Is there a preference to either method?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

They are not even remotely the same thing, so I don't see any situation where one would need to consider preferring one over the other.

Care to elaborate on what you really need to know?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1462
    
    1
Bear Bibeault wrote:They are not even remotely the same thing, so I don't see any situation where one would need to consider preferring one over the other.

Care to elaborate on what you really need to know?


Maybe I should have stated when is it best to use one or the other?

I have noticed that since I started using jQuery 1.8.2 code that I had in the form of a load with callback stoped working. So I used the document ready method to fire the same js function that the callback was fireing and this seems to fix the issue.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

The document ready handler should be used to defer the execution of script until after the DOM has been built. This should be used for any code that tries to do anything at all with DOM elements; you can't find or manipulate elements until they actually exist.

The .load() method is used to make an Ajax request to the server that will return an HTML fragment to be inserted into the DOM.

Again, comparing these is like asking which is better: a chain saw or a puppy toy? They're not even remotely the same thing so I'm still unsure about why they are being conflated.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Steve Dyke wrote:
I have noticed that since I started using jQuery 1.8.2 code that I had in the form of a load with callback stoped working.

Sure. If you were using .load() when you should have been using a ready handler, you had set up a race condition that was bound to catch up with you sooner or later.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1462
    
    1
Bear Bibeault wrote:
Steve Dyke wrote:
I have noticed that since I started using jQuery 1.8.2 code that I had in the form of a load with callback stoped working.

Sure. If you were using .load() when you should have been using a ready handler, you had set up a race condition that was bound to catch up with you sooner or later.


I write code every day and I am sure it is probably 90% wrong but it does most of the time act like it is doing the right thing. Every day I learn more and try to incooporate why I learn into my code. I remember when I first took this task of software develpoment(lol) for our intranet I knew nothing about java and it took three months to get the first page to show any of the simplist desired results. Now I have more than 500,000 lines of code supporting 12+ applications that you could probably do in a quarter of that many lines.

But none of this would have been possible without your help. Thank you for your patience with me. God Bless.
 
GeeCON Prague 2014
 
subject: jQuery load with Callback versus Document Ready Method