Monday 12 May 2008

Getting back to the URL you where at when you opened a document.

I was explaining this to a junior dev today and thought i would pass it on.

If you have a Web App with one form but 3 views and the document can be opened from any one of the 3 views. How do you get back to the view you where at when you opened the document in the first place?

There are several ways of doing this, this is mine and it uses a session cookie in the browser to do it.

First you need some cookie handling JS in your form or external JS file, Something like this.


function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name)
{
createCookie(name,"",-1);
}

Then on the page/form/view that has a link to a document on it place this is in the onLoad event:

createCookie("goback",location.href,0)

This will create a session cookie (it deletes itself when the current browser window closes) with the current location stored in the cookie.

Then on your form, place a hidden field called BackURL, or something similar, then on the onLoad event of the form have the following

document.forms[0].BackURL.value = readCookie("goback");


This pulls back the last place you were from the browser and stores it on the form.
Finally on your WQS agent you can use this to get back to your original place by

print |[script language='javascript']location.href='|+ThisDoc.BackURL(0)+|';[/script]|

Alternately you could populate a $$Return field with the contents of the cookie and use that
to redirect the form to the place that you were.

Disqus for Domi-No-Yes-Maybe