AJAX - The Complete Reference

(avery) #1

PART II


Chapter 9: Site and Application Architecture with Ajax 463


if (key != "AjaxTCRinit")
{
window.location.hash =AjaxTCR.data.encodeValue(key).replace(/%2F/g, '/');
AjaxTCR.history._currentState = window.location.hash.substring(1);
}

Next, a variable is initialized that will store an encoded form of the request options, and
the state value is changed into something that can be stored safely.

var optionsString = "";
var safeStateName = key.replace(/[%\+]/g, "");

Next, there is a simple step: the page title is updated to the passed value.

if (title)
document.title = title;

Now the function checks to see if options were sent or if data was specified. In either case,
the options will be encoded using base64 and stored in the variable optionsString. You’ll
have to do a little work to address responseXML issues in IE. The responseXML is overridden
and set to its serialized form because of an inability to encode this data in that browser.

if (options)
{
options.url = url;
if (navigator.userAgent.toLowerCase().indexOf("msie")>-1)
options.responseXML = options.responseText;
optionsString = AjaxTCR.data.encode64(AjaxTCR.data.encodeJSON(options));
}
else if (data)
{
options = {value: data};
optionsString = AjaxTCR.data.encode64(AjaxTCR.data.
encodeJSON(options));
}

Next, a counter showing the position in the history is updated.

//update position
AjaxTCR.history._historyPosition++;

If you have had trouble following until now, hold on tight: you are about to enter
Internet Explorer’s house of hacks. We find our hacked-in iframe from before, and its
contents are set to include a behavior that holds persistent data and the <div> tag that has
the _currentState value in it, and this is written to the frame. This change of frame
contents is going to add an entry to IE’s history.

if(navigator.userAgent.toLowerCase().indexOf("msie")>-1)
{
var iframe = document.getElementById("ieFix");
var html = '<html><head><title>IE History</title><STYLE>.userData
{behavior:url(#default#userdata);}</STYLE></head><body><div class="userData"
id="persistDiv"></div><div id="currentState">' + AjaxTCR.history._current-
State + '</div></body></html>';
Free download pdf