Hi
I wonder if anyone can help. I want to fire up a pop up when my page loads but I want to give the end-user the ability to click 'don't show again' in the pop up and set a cookie.
However the code below is returning errors, I wondered if anyone could offer advice - I have explained in detail below what I am trying to do.
/************* Index page ******************************/
1. Call the function detectCookie() when the page loads and send it the argument 'my_popup_preference'
2. The function detectCookie() takes the argument 'my_popup_preference' and first tests whether a cookie exists. If there are no cookies it calls the function showPopUp () which opens the popup.
3. If cookies are detected it searches for the cookie name 'my_popup_preference'. If there was a cookie of that name it returns false and the popup window isn't called.
4. Otherwise no cookie of that name was found so call the function showPopUp (); and return false.
5. The function to show the popup window, which is only called if the user has a cookie with the name 'my_popup_preference'
/************* Pop up page ******************************/
6. A link calls the setCookie() function
7. The setCookie function creates a cookie with the name 'my_popup_preference' with an expiry date and allowing the cookie to be referenced by any files in the root server (path=/
and any servers in the .open.ac.uk domain (domain=open.ac.uk
. It then closes the window.
I wonder if anyone can help. I want to fire up a pop up when my page loads but I want to give the end-user the ability to click 'don't show again' in the pop up and set a cookie.
However the code below is returning errors, I wondered if anyone could offer advice - I have explained in detail below what I am trying to do.
/************* Index page ******************************/
1. Call the function detectCookie() when the page loads and send it the argument 'my_popup_preference'
Code:
<body onLoad="detectCookie('my_popup_preference')">
2. The function detectCookie() takes the argument 'my_popup_preference' and first tests whether a cookie exists. If there are no cookies it calls the function showPopUp () which opens the popup.
Code:
function detectCookie(name) {
if (document.cookie == '') {
// there's no cookie, so open the popup and return false;
function showPopUp ();
return false;
}
3. If cookies are detected it searches for the cookie name 'my_popup_preference'. If there was a cookie of that name it returns false and the popup window isn't called.
Code:
// otherwise there is a cookie or multiple cookies
var firstChar, lastChar;
var theBigCookie = document.cookie;
firstChar = theBigCookie.indexOf(name);
// find the start of 'name' - the position of the first character in the string
if(firstChar != -1) {
// if you found the cookie
firstChar += name.length + 1;
// skip 'name' and '='
lastChar = theBigCookie.indexOf(';', firstChar);
// Find the end of the value string (i.e. the next ';') - the last character in the string
if(lastChar == -1) lastChar = theBigCookie.length;
//put the name of the cookie searched for into the variable the_cookie
var the_cookie = theBigCookie.substring(firstChar, lastChar);
return false;
}
4. Otherwise no cookie of that name was found so call the function showPopUp (); and return false.
Code:
else {
// If there was no cookie of that name, open the popup and return false.
function showPopUp ();
return false;
}
}
}
5. The function to show the popup window, which is only called if the user has a cookie with the name 'my_popup_preference'
Code:
function showPopUp () {
window.open('popup.htm','openwindow','width=400,height=400,scrollbars=yes,status=yes resizable=yes');
}
/************* Pop up page ******************************/
6. A link calls the setCookie() function
Code:
<p><a href="#" onclick="setCookie(); return false">Don't show again</a></p>
7. The setCookie function creates a cookie with the name 'my_popup_preference' with an expiry date and allowing the cookie to be referenced by any files in the root server (path=/
Code:
function setCookie () {
// get the information
//
var the_name = "dont_show";
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
// build and save the cookie
var the_cookie = "my_popup_preference=" + the_name;
the_cookie = the_cookie + ";expires=" + the_cookie_date;
the_cookie = the_cookie + "path=/;";
the_cookie = the_cookie + "domain=open.ac.uk;";
document.cookie = the_cookie;
//close the window
window.close();
}