Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jQuery load with Callback versus Document Ready Method

 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a preference to either method?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic