|
|
|
|
|
|
| |
The Problem:
The client had a popup window for news and announcements that displayed each time the index page was loaded. The client wanted to the popup to only be displayed the first time the index page was called.
The Solution:
I wrote a Javascript that tested for a cookie each time the page was loaded. If the cookie had been set nothing happens. If the cookie is not set the code sets the cookie and displays the popup.
Here is the code:
| // ++++++++++++++++++++++++++++++++++++++++++
// Run Once Per Session
//
// Replace the alerts by functions that need to
// be run once per session.
//
// Written by: Michael Regan
// Website : www.owt4nowt.ca
//
// Released under the GPL.
// ++++++++++++++++++++++++++++++++++++++++++
var key_value = "myTestCookie=true";
var foundCookie = 0;
// Get all the cookies from this site and store in an array
var cookieArray = document.cookie.split(';');
// Walk through the array
for(var i=0;i < cookieArray.length;i++)
{
var checkCookie = cookieArray[i];
// Remove any leading spaces
while (checkCookie.charAt(0)==' ')
{
checkCookie = checkCookie.substring(1,checkCookie.length);
}
// Look for cookie set by key_value
if (checkCookie.indexOf(key_value) == 0)
{
alert("Found Cookie ");
// The cookie was found so set the variable
foundCookie = 1;
}
}
// Check if a cookie has been found
if ( foundCookie == 0)
{
// The key_value cookie was not found so set it now
document.cookie = key_value;
alert("Setting Cookie");
} | | |
|
| Store, retrieve and delete cookies using JavaScript. Categories : Java Script, Cookies, Beginner Guides, Cookies | | | Simple Javascript CSS Digital Clock Categories : Java Script, Date Time, CSS, Beginner Guides, Web Design | | | A very simple PHP single password cookie based login without usernames. Categories : PHP, Cookies, Security, Beginner Guides | | | Validating a URL with JavaScript RegExp Categories : Java Script, Data Validation, Beginner Guides | | | Easy alert box pop-up function Categories : PHP, Java Script, Beginner Guides | | | Show hide table rows to make dynamic forms Categories : Beginner Guides, Java Script, Form Processing, HTTP | | | Ajax - Perform a simple server side request and update the current HTML page. Categories : AJAX, Java Script, Beginner Guides | | | Form Processing : with alert Highlight field name which is not filled by user Categories : Java Script, Form Processing, Data Validation, Beginner Guides, Web Design | | | Local Time clock and Server time usign PHP and JavaScript Categories : PHP, Java Script, Date Time, Beginner Guides | | | Function that allows a Javascript cookie to be set after HTML has been outputted to the page.
Categories : PHP, Java Script, Cookies, HTML and PHP | | | Conditional Check - a script that allows a user to submit a form only if the user check a checkbox. Categories : HTML, Java Script, Form Processing, Beginner Guides | | | PHP and javascript mouseover, mouseout, and mousedown events Categories : PHP, Java Script, Form Processing, Beginner Guides | | | Simple Cookie example Categories : PHP, Beginner Guides, Cookies | | | Get your browser details using javascript Categories : Java Script, Beginner Guides, Browsers | | | Changing the Style of form objects using the JavaScript OnClick method. Categories : Java Script, Form Processing, Beginner Guides, CSS | |
|
|
|