1

thanks for looking,

I've got a situation where I want to .load() multiple divs from a static file. I recently found and read this SO question: jquery-multiple-load-in-a-div. The options were either

$('<div>').load('static.html #div1,#div2,...');

or

$.get('static.html',...,function(){ 
    $responseHTML.find('#div1').appendTo(...);
    $responseHTML.find('#div2').appendTo(...);
});

I'm pefectly happy to use either, but I wanted to know a bit more about the theory behind it. Am I right in thinking that '.load()' is probably doing the exact same thing as the '.get()' behind the scenes? Does that mean the .get() is faster?

Any other insights would be appreciated. CB.

Community
  • 1
  • 1
Bung
  • 259
  • 2
  • 7
  • 3
    http://api.jquery.com/load/ "it is roughly equivalent to $.get(url, data, success)". There's not going to be any substantive difference. Regardless - the amount of time it takes to perform an HTTP get over the internet will be orders of magnitude longer than anything that happens once the data is on your side. There's nothing to worry about here as far as the minutae of how "load" is implemented. – Jamie Treworgy Apr 25 '12 at 17:48
  • Seems obvious to me that one network request is always faster than multiple network requests. – mellamokb Apr 25 '12 at 17:50
  • @mellamokb there's only one request either way – Jamie Treworgy Apr 25 '12 at 17:50
  • @jamietre: I don't read anywhere on the documentation that `.load` is cached if you access the same page twice. Does that happen automatically at the browser level? – mellamokb Apr 25 '12 at 17:52
  • .. where do you see the same page being accessed twice? – Jamie Treworgy Apr 25 '12 at 17:52
  • Nevermind, I see you're right. There is only one either way. I interpreted the `#div1,#div2,...` as shorthand for multiple `.load` requests due to the example linked in the OP. I guess there is no reason why it shouldn't be possible to use a compound selector and retrieve them all at once. – mellamokb Apr 25 '12 at 17:53
  • Well, I don't know if it's normal to leave the question or not if I answer it myself, but after some more research I found some help on this. If you check out the source http://james.padolsey.com/jquery/#v=git&fn=jQuery.fn.load and http://james.padolsey.com/jquery/#v=git&fn=jQuery.get ... both functions are just wrappers for an .ajax() call. I suppose the speed difference from wrapping is negligible, so I guess it's just personal coding style preference in this case. – Bung Apr 25 '12 at 17:57
  • You could post that as an answer and accept it (after the required wait period) so the question has closure. – mellamokb Apr 25 '12 at 17:58
  • @mellamokb, yea I interpreted it as multiple .load()s initially as well, which is why I asked, so I guess the title is misleading, sorry :) – Bung Apr 25 '12 at 17:58

1 Answers1

0

(Answering my own question after help from comments.)

If you check out the source http://james.padolsey.com/jquery/#v=git&fn=jQuery.fn.load and http://james.padolsey.com/jquery/#v=git&fn=jQuery.get ... both functions are just wrappers for an .ajax() call. The speed difference from wrapping is negligible, so I guess it's just personal coding style preference in this case.

Note, I also misunderstood originally that doing $.load('file #div1,#div2,..') is not the same as $.load('file #div1');$.load('file #div2');... so the OP title is misleading.

Thanks for everyone's help.

Bung
  • 259
  • 2
  • 7