|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
DisclaimerThe author does not accept responsibility for any effects, adverse or otherwise, that this code may have on you, your computer, your sanity, your dog, and anything else that you can think of. Use it at your own risk. (Deja Vu? I also love the Allegro disclaimer.) Quick and dirty tourSo you don't like to read all articles you see on the web, or you don't have time for it? Then, just download the source files, and open the " pageCookieDemo.htm" file on your favorite browser. BackgroundAlthough the inner works require you to be an advanced web developer to understand and improve it, you need only to have basic knowledge of HTML and JavaScript to use the tool. IntroductionSometimes you need to redirect the user from the current page to accomplish a part of a task. Most people use pop-up windows to refrain from doing this, but sometimes you just can't do it any other way. Anyway, keeping the page controls state between navigation is indeed a useful tool for your bat belt. Keeping the state (and the user)The tool is composed of two js files, one handles browser cookies and the other is a specialization for page state handling. <html>
<head>
<title>Debug Demo</title>
<script type="text/javascript" src="debuggingtools.js "></script>
<script type="text/javascript" src="Cookie.js"></script>
<script type="text/javascript" src=" PageCookie.js"></script>
...
</head>
Please refer to JavaScript Debugging Tool article for information about it. The CookieYou may have seem dozen different cookie handling functions around. Despite the fact the object-oriented style coding, there ain't much news about how to handle a cookie. First, we create the cookie class that will handle all data. var Cookie = function(name, value, path, expires, domain, secure)
{
this.Name = name;
this.Value = value;
this.Path = path;
this.Expires = expires;
this.Domain = domain;
this.Secure = secure;
}
Now we add some control with the JavaScript way to do static methods: prototype. Remember that no cookie can be larger than 4KB. Trying to save a cookie larger than that would generate an empty cookie on most browsers so, to avoid headaches, we will throw an exception when it occurs. Cookie.prototype.MaxSize = 4000; //size in KB
Cookie.prototype.toString = function()
{
return this.Value;
}
Cookie.prototype.Append = function(newValue)
{
this.Value += newValue;
}
The alert( myCookie.Value );
alert( myCookie );
Since I don't want you to bother on how to extract the value of a cookie, the internal function
<button onclick="ShowMe('loadCookie');" ID="Button1">loadCookie</button>
...
function ShowMe(type)
{
try
{
switch(type)
{
case "loadCookie":
alert( new Cookie("demo").Load() );
break;
case "saveCookie":
new Cookie("demo", "myValue").Save();
alert("saved.");
break;
case "deleteCookie":
new Cookie("demo").Delete();
alert("deleted.");
break;
}
}
catch(ex)
{
debugHelper.Exception(ex.message, document.URL,
document.lastModified, document.referrer);
}
}
...
The Page CookieSince there's a limit of 20 cookies per domain (URL), you can't save the state of each control in a separated cookie_name:cookie_value style. Using the page cookie is straightforward.
...
function ShowMe(type)
{
try
{
switch(type)
{
case "GetPageState":
var pc = new PageCookie( document.URL);
pc.GetPageState();
pc.Save();
alert("saved.");
break;
case "RestorePageState":
var pc = new PageCookie(document.URL);
pc.Load();
pc.RestorePageState();
alert("restored.");
break;
case "DeletePageState":
new PageCookie(document.URL).Delete();
alert("deleted.");
break;
case "RefreshPage":
window.location = window.location;
break;
}
}
catch(ex)
{
debugHelper.Exception(ex.message, document.URL,
document.lastModified , document.referrer);
}
}
All we do here is to specialize the var PageCookie = function(pageName)
{
this.base = Cookie;
this.base(pageName);
this.PageName = pageName;
}
PageCookie.prototype = new Cookie();
PageCookie.prototype.constructor = PageCookie;
Since this article is for beginners, I do not intend to explain the inner works of PersistenceA quick word about cookie persistence is recommended here. You may have noticed that, by default, the cookie is session based. This means that if you close the browser, the cookies will be lost. If you need to persist cookies for a period of time, you need to change the cookie property Password and File fields (oh my)"There's always a catch", said Constantine once. And it is true for cookie also. By security reasons, you cannot get a input type Password field value by JavaScript code. At last, not doing it the simple way, and it is really *not* recommended to store sensitive data on cookies. Another interesting field is the input type file, used to upload files to a server. This field value property is read-only, which means that no script can capture undesired files on your machine and upload it to even more undesired servers. A word about cross-domain scriptingI think it is necessary to add some comments about some caveats when using cookies. By default, if you create a cookie on a page inside [www.asp.net/ajax] this cookie is invisible for other web locations like [www.asp.net]. You can work around this by setting the cookie property var PageCookie = function(pageName)
{
this.base = Cookie;
this.base(pageName);
this.Path = "/";
//new line added to set cookie path
this.PageName = pageName;
}
Also, all cookies have a ...
var pc = new PageCookie(document.URL);
pc.Domain = "myDomain.com"; //use common domain for all cookies
pc.GetPageState();
pc.Save();
...
Note that I did *not* change any of this behaviors on this article source-code. Those are the default actions of a browser and you should change them for your own specific needs when required. ConclusionAnd that's it for the JavaScript page cookie tool! I hope you find many uses for it. ReferencesSome great resources on the web:
History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||