Download example scripts - 1 Kb
Introduction
This article follows the great JavaScript for Beginners
article and hopes to bring its readers to the cruel reality of life by
discussing cookies .
The problem I wanted to solve was "automated" access to
http://www.thehungersite.com.
This page has the ability to limit you to one visit per day only (don't forget
to click on the link above!). Until now, each time I first started my browser I
would manually choose a bookmark to
load the page.
Why not create simple script for it?
Because I wanted to have it work under Netscape as well as IE I started to study
JavaScript.
The Solution
Main idea is simple: create a page what will determine whether it has already
been loaded today, and if not make it switch to http://www.thehungersite.com,
and set this page as browser's home page.
Getting the actual page and redirecting is easy. The problem was remembering.
Because JavaScript has
no file access functions (and looking into examples like http://www.javascript.sk/skript.php3?150, no vector
graphics either)
we must use so called cookies. (also see MSDN - Platform SDK/Tools and
Languages/Scripting/JScript/FileSystemObject User's Guide/Working with Files.
There are examples for file access from JScript using ActiveX objects, but of course
this is only good for Microsoft browsers).
Cookies are size-limited variables
associated with a server's domain.
By default cookie expire when the browser closes (not when you leave the page!)
but a script programmer can change this.
Persistent cookies are stored at the users end, separately for every browser user
and separately for every browser (Netscape uses for cookies in file, Internet
Explorer stores every cookie in a separate one). This difference in browsers brings surprises at some servers where you
must log in again and again Be aware that a user can disable cookies in his or
her browser settings.
For a detailed cookie description you can see http://www.netscape.com/newsref/std/cookie_spec.html.
Believing this all is true I found and used examples for cookies using at http://www.javascript.sk.
All was fine and nice until time I not tried it with Internet Explorer. Then after
I called some example Javascript:
cookieExpires = "01-APR-" + nLyear + " GMT";
document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
and then called
document.write(document.cookie);
document.cookie
was empty.
After experiments and searches in examples I found that
Knowing this, the rest was without problems:
(See attached file: homepage.htm)
Conclusion
There is not a single JavaScript only.
If you want use it in your web pages you must minimally test it with different
browsers - and their versions.
Many scripts contain browser type and version checking and lots of if-the-else's
to handle the differences..
Short vocabulary for http://www.javascript.sk
ukazka |
demonstration |
zdrojovy kod |
source code |
prirucka |
manual |
priklad(y) |
example(s) |
You can see source for
browser identification examples at http://www.javascript.sk/prirucka_sk/cast5.html.