rest/ahah: Difference between revisions
No edit summary |
|||
Line 10: | Line 10: | ||
== Relation to AJAX == | == Relation to AJAX == | ||
AHAH is intended to be a much simpler way to do [http://en.wikipedia.org/wiki/Web_development web development] than [http://en.wikipedia.org/wiki/Ajax_ | AHAH is intended to be a much simpler way to do [http://en.wikipedia.org/wiki/Web_development web development] than [http://en.wikipedia.org/wiki/Ajax_(programming) AJAX]: "Asynchronous JavaScript and XML." Strictly speaking, AHAH can be considered a subset of AJAX, since (X)HTML is just a special kind of XML. However, it is a subset with some very specific and useful properties: | ||
# The lack of custom XML schemas dramatically reduces design time | # The lack of custom XML schemas dramatically reduces design time | ||
# AHAH can trivially reuse existing HTML pages, avoiding the need for a custom web service | # AHAH can trivially reuse existing HTML pages, avoiding the need for a custom web service | ||
Line 25: | Line 25: | ||
Unlike the various libraries (e.g., [http://en.wikipedia.org/wiki/JSON JSON], [http://mochikit.com/ MochiKit]) important for AJAX, all of AHAH is contained in a single JavaScript file (also available as [http://www.opendarwin.org/~drernie/src/ahah.js ahah.js] and [http://homepage.mac.com/kevinmarks/jah.js jah.js]). In fact, this is little more than the canonical XMLHttpRequest example, and is simple enough for any modern web designer to embed within their existing web pages. | Unlike the various libraries (e.g., [http://en.wikipedia.org/wiki/JSON JSON], [http://mochikit.com/ MochiKit]) important for AJAX, all of AHAH is contained in a single JavaScript file (also available as [http://www.opendarwin.org/~drernie/src/ahah.js ahah.js] and [http://homepage.mac.com/kevinmarks/jah.js jah.js]). In fact, this is little more than the canonical XMLHttpRequest example, and is simple enough for any modern web designer to embed within their existing web pages. | ||
NOTE: The example ahah.js mentioned here has the unfortunate requirement (if the delay argument is not undefined) that url,target,delay must be global variables (which makes one wonder why they are passed as parameters....). If you don't want to use globals, and want this to actually work, use something like: setTimeout( 'ahah( "' | NOTE: The example ahah.js mentioned here has the unfortunate requirement (if the delay argument is not undefined) that url,target,delay must be global variables (which makes one wonder why they are passed as parameters....). If you don't want to use globals, and want this to actually work, use something like: setTimeout( 'ahah( "' url '", "' target '", ' delay ')', delay ); Fair warning. | ||
=== Send AHAH Request === | === Send AHAH Request === | ||
Line 58: | Line 58: | ||
document.getElementById(target).innerHTML = results; | document.getElementById(target).innerHTML = results; | ||
} else { | } else { | ||
document.getElementById(target).innerHTML="ahah error:\n" | document.getElementById(target).innerHTML="ahah error:\n" | ||
req.statusText; | req.statusText; | ||
} | } | ||
Line 80: | Line 80: | ||
var st = node.getElementsByTagName('SCRIPT'); | var st = node.getElementsByTagName('SCRIPT'); | ||
var strExec; | var strExec; | ||
for(var i=0;i<st.length; i | for(var i=0;i<st.length; i ) { | ||
if (bSaf) { | if (bSaf) { | ||
strExec = st[i].innerHTML; | strExec = st[i].innerHTML; | ||
Line 94: | Line 94: | ||
} | } | ||
try { | try { | ||
eval(strExec.split(" | eval(strExec.split(" | ||
Revision as of 17:00, 22 April 2007
AHAH: Asychronous HTML and HTTP
AHAH is a very simple technique for dynamically updating web pages using JavaScript. It involves using XMLHTTPRequest to retrieve (X)HTML fragments which are then inserted directly into the web page, whence they can be styled using CSS.
Contributors
Relation to AJAX
AHAH is intended to be a much simpler way to do web development than AJAX: "Asynchronous JavaScript and XML." Strictly speaking, AHAH can be considered a subset of AJAX, since (X)HTML is just a special kind of XML. However, it is a subset with some very specific and useful properties:
- The lack of custom XML schemas dramatically reduces design time
- AHAH can trivially reuse existing HTML pages, avoiding the need for a custom web service
- All data transport is done via browser-friendly HTML, easing debugging and testing
- The HTML is designed to be directly embedded in the page's DOM, eliminating the need for parsing
- As HTML, designers can format it using CSS, rather than programmers having to do XSLT transforms
- Processing is all done on the server, so the client-side programming is essentiall nil (moving opaque bits)
In fact, for any content that is destined to be viewed by the browser, it is virtually impossible to imagine any advantage to sending it as custom XML rather than structurally-correct HTML (with appropriate CSS-friendly class names, of course).
That said, many applications of AJAX are (at least in theory) targeteable at custom JavaScript code or desktop GUIs rather than mere browsers. For those cases, the advantages of HTML over custom XML are somewhat less. However, even here, it may well make sense to encode data using xoxo -- aka XHTML Property Lists -- which can be losslessly converted back and forth from standard data structures (lists and dictionaries) without the need for custom parsers.
Source Code
Unlike the various libraries (e.g., JSON, MochiKit) important for AJAX, all of AHAH is contained in a single JavaScript file (also available as ahah.js and jah.js). In fact, this is little more than the canonical XMLHttpRequest example, and is simple enough for any modern web designer to embed within their existing web pages.
NOTE: The example ahah.js mentioned here has the unfortunate requirement (if the delay argument is not undefined) that url,target,delay must be global variables (which makes one wonder why they are passed as parameters....). If you don't want to use globals, and want this to actually work, use something like: setTimeout( 'ahah( "' url '", "' target '", ' delay ')', delay ); Fair warning.
Send AHAH Request
function ahah(url,target) { document.getElementById(target).innerHTML = 'loading data...'; if (window.XMLHttpRequest) { req = new XMLHttpRequest(); req.onreadystatechange = function() {ahahDone(target);}; req.open("GET", url, true); req.send(null); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); if (req) { req.onreadystatechange = function() {ahahDone(target);}; req.open("GET", url, true); req.send(); } } }
Note the workaround needed for IE's ActiveX implementation. The current version hard-codes GET; there may be value in adding an extra parameter to allow POST, PUT, and DELETE.
Receive AHAH Request
function ahahDone(target) { // only if req is "loaded" if (req.readyState == 4) { // only if "OK" if (req.status == 200 || req.status == 304) { results = req.responseText; document.getElementById(target).innerHTML = results; } else { document.getElementById(target).innerHTML="ahah error:\n" req.statusText; } } }
Other than error checking, the only real work done is:
document.getElementById(target).innerHTML = results;
Everything else (e.g., CSS-styling) is merely inherited from the parent webpage.
Executing Javascript
Since the browser won't execute <script> tags when changing innerHTML, you may want to apply the following function call on document.getElementById(target):
var bSaf = (navigator.userAgent.indexOf('Safari') != -1); var bOpera = (navigator.userAgent.indexOf('Opera') != -1); var bMoz = (navigator.appName == 'Netscape'); function execJS(node) { var st = node.getElementsByTagName('SCRIPT'); var strExec; for(var i=0;i<st.length; i ) { if (bSaf) { strExec = st[i].innerHTML; } else if (bOpera) { strExec = st[i].text; } else if (bMoz) { strExec = st[i].textContent; } else { strExec = st[i].text; } try { eval(strExec.split("