Sunday, April 20, 2008

Ajax Refresh Problem

Lot of people complain that they have tough time with the Refresh Key of browser when using (JavaScript) AJAX. Well, there is a very easy workaround to get rid of same.

 

This is a two step process...

 

Let's take a scenario where you have a page (say default.html) and you call the AJAX function (say getPage()) to load the first default page into container. Now in first step, you need to modify your default call to getPage() from default.html...

 

<script language="javascript" type="text/javascript">

var Crawl = window.location.hash;

if (Crawl.length > 1)
{
    Crawl = Crawl.replace('#','');
    Crawl = Crawl + window.location.search;       
     getPage(Crawl);
}
else
{
    getPage(default.asp');
}

 

</script>

 

But this is only one part. Another important part in inside the getPage() function.

 

When you call getPage() function, simply set a global variable (say Hash) to the page being called.

 

i.e. if you the function prototype is function getPage(PageName) {}, write command...

function getPage(CallPage) {

     Hash = CallPage;

}

 

Now within this function, you must be calling another function (say handleHttpResponse) on onreadystatechange event. Inside this function, set the HASH value of page by writing command...

window.location.hash = Hash;

 

And bingo!!!! You are done. Now your F5/Refresh problem is solved. If you need any more details on this, feel free to contact me at vikramjit.rooprai@gmail.com

0 comments: