Pros and con of $.ajax, $.load, $.getscript and object tag

Go To StackoverFlow.com

4

I am using strict doctype and i want to embed a page within a page, for this i cannot use iframe as the doctype is strict , so found out 4 methods:

  • $.ajax()
  • .load()
  • $.getscript
  • <object> tag

can any one please tell me about the pros and con of all these approaches..

Thanks

2012-04-04 07:27
by Kunal Vashist


5

Ok, i'll attempt to answer this one.

  • $.ajax() is the root method of jQuery for ajax requests. it is so detailed, you have a lot of parameters to configure it (mostly, they are left as default). this complexity gave rise to the common $.get() and $.post() shorthands for easy use. $.ajax() is what you use if you want fine-grained control of the ajax request and what you want to do with it after.

  • .load() is a "subfunction" of $.get() (it uses get) but is has an implicit callback (aside from an optional callback). it instead loads the requested page, and places it in the element that precedes it as an escaped mark-up. this is good for loading pages that are already formatted to load into the target container.

  • .getScript() is NOT meant to load pages. it's to retrieve scripts and execute them on the page.

  • <object> tags however...

    The object element’s purpose is to embed into a document a variety of different kinds of media files. Historically, it was used primarily for placing ActiveX controls onto a page, but it can also be used to embed images (.gif, .jpg, and so on), movie files and applets, video files, PDF documents, Flash, and even HTML.

    although it can render HTML, it's not it's original purpose. It's meant for other media.


personally, i go for $.ajax() or $.get to return JSON data and an HTML template, then have a template engine to build the page. this way, JSON is light on the bandwidth, and templates are cacheable.

2012-04-04 08:00
by Joseph
+1 on using JSON data + HTML template vs embedding HTML. Unless it's partial HTML fragments I can't see how embedded HTML is going to validate - booyaa 2012-04-04 08:32
Update: consider ES7 async functions (await) too - Leo 2016-12-08 21:04
Ads