AJAX vs. AHAH Is there a performance advantage?
My concern is performance, is there any reason to send client-side XML instead of valid HTML? Like most things, I'm pretty sure it depends on the application. My particular situation is that substantial content has been inserted into the web page that was pulled from the database.
What are the benefits of any approach? Is content size even an issue? Or, in the case of using XML, would the JavaScript time to process the XML in balancing HTML the additional time it would take to send the HTML to begin with?
Thanks, Jeff
a source to share
It's important to understand that even when choosing HTML, you shouldn't just insert it directly into the DOM without processing it. If it is not transmitted over a secure protocol, it can be subject to MITM attacks. Every time the stored XML parsing is spent sanitizing the HTML to prevent script injection.
XML is more portable. The data used in XML can be used anywhere on any page. HTML is different in this aspect (although it can be generated with CSS), not all browsers have an easily accessible parser without inserting content into the DOM. Some may have DOMDocument()
, but you cannot rely on it for cross-browser. With XML, you can also customize and retrieve the data you need effortlessly using XPath. With HTML (x-browser), you need to insert into the DOM first or use the always invalid regexp method, which should never be used.
JSON is lighter, it comes with a lot of the bulk that comes with XML and HTML. As native markup for JavaScript objects, it is also very easy to parse. Unlike XML and HTML, you can also access cross-domain JSON data using JSON with Padding (JSONP).
So, you need to choose the method that best suits your needs. Most people tend to go with JSON these days because it is very light weight, cross domain access, and requires very little effort to parse with JavaScript.
a source to share
AJAX hasn't been strictly enforced behind their acronyms for years. It's just a nickname for "asynchronously loaded content".
Most AJAX these days work with JSON.
Whether you are sending HTML as your data via JSON or anything else (even XML) really depends on the specific needs of your application. In this respect, AHAH is just a subset of AJAX.
If there is no benefit to the fact that the data is parsed / rendered on the client side, then just do it on the server side and return the HTML.
a source to share
JSON (Javascript Object Notation) is more commonly used than XML because it is lightweight and native Javascript.
That being said, if you need XML, you can override XML.
Here's a good page on when to use HTML, XML or JSON
Here's a good page on the differences between the two.
JSON is mainly used to improve performance. To use the example from this page :
XML:
<?xml version=’1.0′ encoding=’UTF-8′?>
<card>
<fullname>Bala Arjunan</fullname>
<org>PH</org>
<emailaddrs>
<address type=’work’>abcd@xyz.com</address>
<address type=’home’ pref=’1′>me@mailme.com</address>
</emailaddrs>
<telephones>
<tel type=’work’ pref=’1′>+12345678</tel>
<tel type=’mobile’>+1234 1234</tel>
</telephones>
<addresses>
<address type=’work’ format=’B'>1234 Oil Mill St Chennai, IND</address>
<address type=’home’ format=’B'>5678 Oil Mill St Chennai, IND</address>
</addresses>
<urls>
<address type=’work’>http://balaarjunan.wordpress.com/</address>
<address type=’home’>http://balaarjunan.wordpress.com/</address>
</urls>
</card>
JSON:
{
"fullname": "Bala Arjunan",
"org": "PH",
"emailaddrs": [
{"type": "work", "value": "abcd@xyz.com"},
{"type": "home", "pref": 1, "value": "me@mailme.com"}
],
"telephones": [
{"type": "work", "pref": 1, "value": "+12345678"},
{"type": "mobile", "value": "+1234 1234"}
],
"addresses": [
{"type": "work", "format": "us", "value": "1234 Oil Mill St Chennai, IND"},
{"type": "home", "format": "us", "5678 Oil Mill St Chennai, IND"}
],
"urls": [
{"type": "work", "value": "http://balaarjunan.wordpress.com/"},
{"type": "home", "value": "http://balaarjunan.wordpress.com/"}
]
}
There is much less redundancy with JSON.
OTOH, sending plain ol 'HTML, is very efficient and at times. You have to think about your data. If you are just updating a paragraph of text, just submit the html via. If you are dealing with items or a collection of items that you are going to manipulate or use somehow in Javascript, you want JSON. If you want to ASyncronously update an RSS feed or some other XML request, you are requesting XML.
Remember that HTML is just a subset of XML. and xHTML follows all xml rules. Browsers that know javascript (all of them) can understand JSON (Javascript) and HTML (XML). Choose what suits your project based on how you will use the data.
a source to share