
Solid, safe website tools.
Whatever your need, Will Bontrager builds powerful software solutions.
|
(Get Attention Ticker for your web site)
Do Only OnceBy Will Bontrager Gregory Ross of VIP Dogs asked how one would prevent a "leaving this site" popup from being displayed more than once. This post will show how to prevent pretty much any JavaScript function from running more than once with a cookie. The instructions in the JavaScript tell how to set up to cookie to be available site-wide or only from certain directories. The first thing to do is to put these two lines of JavaScript into the function you want to limit to running only once: Make it so those two lines are the first lines the function runs. For example, if your function was function Something()
{
alert("hello");
}
Then it would become function Something()
{
if(HasCookie()) { return; }
GiveCookie();
alert("hello");
}
That was the first step. The above code will work only when both steps have been completed. What the above code does: If the browser has the cookie, processing returns out of the function. Otherwise, it is given the cookie and the function continues to run. The next step is to insert this JavaScript somewhere above the modified function. It doesn't have to be immediately above, although it can be. The idea is for the browser to load this JavaScript before the modified function is loaded. Instructions in the JavaScript tell you where (and what) to specify the domain name, the section of the web site the cookie shall affect, how long the cookie shall persist, and the cookie's name. Remove the <script...> and </script> tags if the JavaScript will be in a file separate from the web page August 9, 2007 Please note: Articles on this website are presented "as is". However - If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Rate this blog post.
No page reload! |
|
|
© 1998-2001 William and Mari Bontrager |
|