Handling hashed links for web applications using AJAX

Web page visitors may want to bookmark certain state or pass it along to others. In pre-AJAX days it was a matter of simply passing a page URL. With AJAX, however, there's no standard way of conveying page state when some content have been pulled thru an AJAX call.

One solution is to pass a "hash" parameter as if it was a request to move to an anchor within the page. That hash parameter can then be fetched processed by Javascript:


function process_hashed_url(url) {
	var res = url.match(/#(.+)$/);	/* Look for a hash - it's a part between # and the end of URL */
	if (!res)
		return;	/* Skip if not a "hashed" link */
	var hash = res[1];	
	new Ajax.Updater('target','parts/'+hash+'.html',{method: 'get'});	/* Process the hash parameter - in this case, fetch some additional content thru an AJAX call*/
}

process_hashed_url(document.location.href);	/* See if page was requested with a hash parameter */

Click here to see an example and here to see another content fetched. Download example code here.

photo is a part of the Alpha Orionis and does freelance too.

I recommend

Freakonomics Getting Things Done Getting Real Steve Krug: Don't make me think

I'm using

Mac Coda RoR

Hosting is kindly provided by