Friday, January 13, 2012

Dynamic browser redirect with preserving UrlReferrer

The standard way for a dynamic redirect executed from the browser context is via 'window.location = "<runtime derived url>";'. A side effect of this approach is that you have lost the UrlReferrer information after navigation to the second page. The browser namely treats this approach the same as if the user has explicitly entered the url in the location bar. There is no referrer since referrers are about one page directly referring to another (eg. via a link), not the browser just navigating to another page. If you need the UrlReferrer setting - a typical example is for (re)directing the browser to the first page after action performed on the second page -, a simple javascript trick can be used.
<script type="text/javascript">

EditLink2 = function(a,b)
{
    var redirectLink = document.createElement('A');
    redirectLink.setAttribute("href", "<runtime derived url>");
    document.body.appendChild(redirectLink);
    redirectLink.click();
}
</script>

With this code setup, the dynamic derived redirect is added to the context of first page. As result, the UrlReferrer state is repaired and available for inquiry in the (webclient + webserver) context of second page.

No comments:

Post a Comment