Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allowing Cookie Preferences for Pop-ups

Status
Not open for further replies.

steve100

Technical User
Aug 15, 2002
35
0
0
GB
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'

Code:
<body onLoad=&quot;detectCookie('my_popup_preference')&quot;>


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=&quot;#&quot; onclick=&quot;setCookie(); return false&quot;>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=/;) and any servers in the .open.ac.uk domain (domain=open.ac.uk;). It then closes the window.


Code:
function setCookie () {

// get the information
//
var the_name = &quot;dont_show&quot;;
var the_date = new Date(&quot;December 31, 2023&quot;);
var the_cookie_date = the_date.toGMTString();

// build and save the cookie
var the_cookie = &quot;my_popup_preference=&quot; + the_name;
the_cookie = the_cookie + &quot;;expires=&quot; + the_cookie_date;
the_cookie = the_cookie + &quot;path=/;&quot;;
the_cookie = the_cookie + &quot;domain=open.ac.uk;&quot;;


    
document.cookie = the_cookie;

//close the window

window.close();


}




 
this is a call to a function, so the word &quot;function&quot; is not allowed:
Code:
else {

// If there was no cookie of that name, open the popup and return false.

[b]function[/b] showPopUp ();
return false;    
}

it should be
Code:
else {

// If there was no cookie of that name, open the popup and return false.

showPopUp ();
return false;    
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top