Using JavaScript Cookies and ASP Session to Maintain Last Page Accessed By Each User
We know that we could just use Request.ServerVariables("URL") to find out the url accessed by user in ASP page. If a user opens more than one ASP page, what should we do? Keep it in a session variable, for example:
Session("lastpage") = Request.ServerVariables("URL") _
& "?" & Request.ServerVariables("QUERY_STRING")
The variable behind the question mark (?) is server variables called query string which is used by calling Request.queryString("the_var_we_made").
So now we already have variable that keeps last pages accessed by user on the server side. Then, I want to use this variable so I can check if the page being viewed is the last page user accessed from the server. May be you would say, hey, the page being viewed IS the last page user accessed. Nope, a user can click the "back" button in a browser, or open a new window and then return to the first page without notices that it's not the last page he/she accessed from the server. And I have a problem with my web services which is the excel data that will be created in my web services is only the last data requested in the last page accessed from the server, coz I use session to keep the last data. My solution to resolve this problem is by keeping a variable on client side using javascript cookies, then give warning to user is the page clicked and lastpage are not the same and reload the page that is clicked by user. Put this code in each page:
<% Session("lastpage") = Request.ServerVariables("URL") _
& "?" & Request.ServerVariables("QUERY_STRING") %>
<SCRIPT type=text/javascript>
<!--
document.cookie="lastpage="+"<% = Session("lastpage) %>";
//-->
</script>
Then, in the create_excel page (To create an excel file, user have to click on a link and then create_excel.asp page will be opened) I put this code:
<SCRIPT type=text/javascript>
<!--
function to_reload()
{
var search2="lastpage=";
offset=document.cookie.indexOf(search2);
if (offset != -1)
{
offset += search2.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
if (document.cookie.substring(offset,end)
!="<% =Session("lastpage") %>")
{
window.opener.location.reload();
alert("Data not ready! Please wait "
+ "until finish reloading the page "
+ "then click 'create excel' again")
window.close();
}
}
}
to_reload();
//-->
</script>
The disadvantage of this solution is, user using browser that doesn't support cookies or the cookies is not enabled can not use this feature. One more thing, if you ask me, why don't you just use Request.Cookies in ASP instead of javascript cookies. Well, that time (when I create the above script) I didnn't know that there is cookies usage in ASP. Silly me :P
0 Comments:
Post a Comment
<< Home