By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

cookie to display pop-up only once during browser session?

P: n/a
Hello,

I have a pop-up window that I would like to appear in front of the
browser home page when a user opens IE. Problem is, I'd like it to
never appear again if the user navigates back to the home page during
their time using the browser. However, if the user closes the
browser, then reopens, the pop-up should appear again. (you may have
guessed that this will be used for public access pc's.) I want to try
as best I can to catch all users. I find that users generally close
the browser when they are finished with their work.

Any guidance would be greatly appreciated.

Thanks,
Chris
Jul 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
In article <b8*************************@posting.google.com> ,
cm*********@nyc.rr.com enlightened us with...
Hello,

I have a pop-up window that I would like to appear in front of the
browser home page when a user opens IE. Problem is, I'd like it to
never appear again if the user navigates back to the home page during
their time using the browser. However, if the user closes the
browser, then reopens, the pop-up should appear again. (you may have
guessed that this will be used for public access pc's.) I want to try
as best I can to catch all users. I find that users generally close
the browser when they are finished with their work.

Any guidance would be greatly appreciated.


Unless you own the home page, you can't do this with javascript.
Even if you do, you still need either cookies or sessions.

Do you own the home page?

If what you really want is a warning to users or some such, like we have
at my work, you can look into setting policies on the computers. Windows
98 and above supports this sort of thing. You would make a little
warning with an HTA, VB, or other program, then whenever IE is opened,
the warning is displayed.

--
~kaeli~
Why did kamikaze pilots wear helmets?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2

P: n/a
thanks kaeli.
Yes, I do own the home page. I figured cookies was the way to go, but
wasn't sure how. I use cookies on one page now that has a pop-up
(once the pop-up shows, the cookie dictates that it should not appear
for another 7 days.) Do you know if the cookie can be tied to the
browser being closed/opened again, as opposed to controlling by time?

Thanks again,
Chris
Jul 20 '05 #3

P: n/a
chrism wrote on 19 Nov 2003:
thanks kaeli.
Yes, I do own the home page. I figured cookies was the way to
go, but wasn't sure how. I use cookies on one page now that has
a pop-up (once the pop-up shows, the cookie dictates that it
should not appear for another 7 days.) Do you know if the
cookie can be tied to the browser being closed/opened again, as
opposed to controlling by time?

Thanks again,
Chris


If you don't specify an expiry date for the cookie, it is deleted
once the browser session ends. You can use this fact like so:

// If no cookie has been set...
if ( !document.cookie.length )
{
// ...show pop-up and set a dummy cookie
document.cookie = 'popup=no';
}

If you use other cookies with explicit expiry dates, you'll have to
actually check if the 'popup' cookie above exists:

if ( -1 == document.cookie.search( /popup=no/i ))
{
// ....
}

There might be a more efficient way of searching for the cookie...

This works under Opera 7.22 and Internet Explorer 6.

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #4

P: n/a
In article <b8*************************@posting.google.com> ,
cm*********@nyc.rr.com enlightened us with...
thanks kaeli.
Yes, I do own the home page. I figured cookies was the way to go, but
wasn't sure how. I use cookies on one page now that has a pop-up
(once the pop-up shows, the cookie dictates that it should not appear
for another 7 days.) Do you know if the cookie can be tied to the
browser being closed/opened again, as opposed to controlling by time?


Session cookies will do what you want. That means no expire date.
On the home page, check for the cookie. If it's not there, popup.

Here's my cookie functions for you. If getCookie() returns "", it's not
set. :)

/* jsCookies.js */
/* This file contains cookie functions. */
/* File Functions:
1. setCookie - writes cookie
2. getCookie - gets value of cookie
3. removeCookie - deletes a cookie
4. detectCookies - checks if cookies are enabled
*/

function setCookie(cookieName, cookieValue, expireDate)
{
/* Pass in three strings - the name of the cookie, the value, and the
expire date.
Pass in a "" empty string for expireDate to set a session cookie
(no expires date).
Pass in any other date for expire as a number of days to be added
to today's date. */

if (expireDate == "")
{
expires = "";
}
else
{
expires = new Date();
expires.setDate(expires.getDate() + expireDate);
expires = expires.toGMTString();
}
document.cookie = cookieName+"="+cookieValue+";expires="+expires;
}

function removeCookie (cookieName)
{
/* Pass in the name of the cookie as a string and it will be removed.
*/
expires = Now();
document.cookie = cookieName+"= ;expires="+expires.toGMTString();
}

function getCookie (cookieName)
{
cookieValue = ""
if (document.cookie.indexOf(cookieName) == -1)
{
// there is no cookie by this name for this user
return cookieValue;
}
else
{
// get the beginning index of the cookie by looking for the cookie
name
cookieStart = document.cookie.indexOf(cookieName);
// get the beginning index of the cookie value by looking for the
equal sign after the name
cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
// get the end index of the cookie value by looking for the semi-
colon after the value
cookieValEnd = document.cookie.indexOf(";", cookieStart);
// if no semi-colon, then use the whole length
if (cookieValEnd == -1)
{
cookieValEnd = document.cookie.length
}
// use substring to get the text between the two indices and that
is the value of the cookie
cookieValue = document.cookie.substring(cookieValStart,
cookieValEnd);
return cookieValue;
}
}

function detectCookies()
{
/* function returns true if cookies are enables, false if not */
setCookie("test", "test", "");
tmp = getCookie("test")
if (tmp != "test")
{
return false;
}
else
{
return true;
}
}
--
~kaeli~
Press any key...NO, NO, NO, NOT THAT ONE!!!!!!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #5

P: n/a

"Michael Winter" <M.******@blueyonder.co.uk.invalid> wrote in message
news:Xn*******************************@193.38.113. 46...
chrism wrote on 19 Nov 2003:
thanks kaeli.
Yes, I do own the home page. I figured cookies was the way to
go, but wasn't sure how. I use cookies on one page now that has
a pop-up (once the pop-up shows, the cookie dictates that it
should not appear for another 7 days.) Do you know if the
cookie can be tied to the browser being closed/opened again, as
opposed to controlling by time?

Thanks again,
Chris


If you don't specify an expiry date for the cookie, it is deleted
once the browser session ends. You can use this fact like so:

// If no cookie has been set...
if ( !document.cookie.length )
{
// ...show pop-up and set a dummy cookie
document.cookie = 'popup=no';
}

If you use other cookies with explicit expiry dates, you'll have to
actually check if the 'popup' cookie above exists:

if ( -1 == document.cookie.search( /popup=no/i ))
{
// ....
}

There might be a more efficient way of searching for the cookie...

This works under Opera 7.22 and Internet Explorer 6.

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)


Hi Mike
I'm learning cookies for my site. I would be grateful if you could explain
some of your code

1. !document.cookie.length - is this saying do something if there is no
cookie, established by the fact that no cookie with a name that is longer
than zero can be found? (may be way off here in my interpretation)
2. document.cookie = 'popup=no'; - is this setting the name of the cookie
to 'popup=no', I think I'm totally off here, that would be a job for
setcookie wouldn't it?
3. if ( -1 == document.cookie.search( /popup=no/i )) - is this something
like if the expression on the right is equivalent to false then do
something. But I am confused about details like the forward slashes and the
letter 'i' at the very end of the line.

thanks for any help
David

Jul 20 '05 #6

P: n/a
David Graham wrote on 20 Nov 2003:
Hi Mike
I'm learning cookies for my site. I would be grateful if you
could explain some of your code

1. !document.cookie.length - is this saying do something if
there is no cookie, established by the fact that no cookie with
a name that is longer than zero can be found? (may be way off
here in my interpretation)
Almost. The document.cookie property returns a String containing
*all* cookie name=value pairs. 'length' above is a String property
that contains the length of the String. The expression evaluates to
true if there are no cookies whatsoever associated with the domain of
your site (document.cookie is zero-length). That is why I said that
if you do use other cookies, this wouldn't be sufficient. It has
other possible pitfalls, so I would recommend the second method.
2. document.cookie = 'popup=no'; - is this setting the name of the
cookie to 'popup=no', I think I'm totally off here, that would be a
job for setcookie wouldn't it?
Yes, this creates a cookie called 'popup' with a value of 'no', and
no expiry date (so it expires when all browser windows are closed).
If the setcookie that you use is like this one below (from Netscape's
JavaScript Guide), then yes, you could use setcookie.

// Sets cookie values. Expiration date is optional
//
function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value)
+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}
3. if ( -1 == document.cookie.search( /popup=no/i )) - is this
something like if the expression on the right is equivalent to
false then do something. But I am confused about details like the
forward slashes and the letter 'i' at the very end of the line.


As I said earlier, document.cookie returns a String, and 'search' is
a method of String. It uses a "regular expression" to find a match
and returns the index of that match. If there is no match, it returns
-1. The method call above will return -1 if the string returned by
document.cookie doesn't contain "popup=no".

The slash syntax is used to create a regular expression literal, just
like quotes create string literals. The 'i' at the end makes the
search case-insensitive. Regular expressions are very powerful and
can be very confusing to look at. You should be able to find detailed
descriptions on how to use them in a good JavaScript reference. If
you're interested, try one of Netscape's guides here:

http://devedge.netscape.com/library/...ript/1.3/guide
/regexp.html

Hope that clears things up. Feel free to ask anything else if not.

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #7

P: n/a
JRS: In article <MP************************@nntp.lucent.com>, seen in
news:comp.lang.javascript, kaeli <ti******@NOSPAM.comcast.net> posted at
Thu, 20 Nov 2003 07:32:55 :-
function detectCookies()
{
/* function returns true if cookies are enables, false if not */
setCookie("test", "test", "");
tmp = getCookie("test")
if (tmp != "test")
{
return false;
}
else
{
return true;
}
}


The following looks simpler to me, and should be equivalent :-

function CookiesEnabled() {
setCookie("test", "Test", "");
return getCookie("test") == "Test" }

Your expireDate is not in fact a date; I suggest expireDays.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8

P: n/a

"Michael Winter" <M.******@blueyonder.co.uk.invalid> wrote in message
news:Xn*******************************@193.38.113. 46...
David Graham wrote on 20 Nov 2003:
Hi Mike
I'm learning cookies for my site. I would be grateful if you
could explain some of your code

1. !document.cookie.length - is this saying do something if
there is no cookie, established by the fact that no cookie with
a name that is longer than zero can be found? (may be way off
here in my interpretation)


Almost. The document.cookie property returns a String containing
*all* cookie name=value pairs. 'length' above is a String property
that contains the length of the String. The expression evaluates to
true if there are no cookies whatsoever associated with the domain of
your site (document.cookie is zero-length). That is why I said that
if you do use other cookies, this wouldn't be sufficient. It has
other possible pitfalls, so I would recommend the second method.
2. document.cookie = 'popup=no'; - is this setting the name of the
cookie to 'popup=no', I think I'm totally off here, that would be a
job for setcookie wouldn't it?


Yes, this creates a cookie called 'popup' with a value of 'no', and
no expiry date (so it expires when all browser windows are closed).
If the setcookie that you use is like this one below (from Netscape's
JavaScript Guide), then yes, you could use setcookie.

// Sets cookie values. Expiration date is optional
//
function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value)
+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}
3. if ( -1 == document.cookie.search( /popup=no/i )) - is this
something like if the expression on the right is equivalent to
false then do something. But I am confused about details like the
forward slashes and the letter 'i' at the very end of the line.


As I said earlier, document.cookie returns a String, and 'search' is
a method of String. It uses a "regular expression" to find a match
and returns the index of that match. If there is no match, it returns
-1. The method call above will return -1 if the string returned by
document.cookie doesn't contain "popup=no".

The slash syntax is used to create a regular expression literal, just
like quotes create string literals. The 'i' at the end makes the
search case-insensitive. Regular expressions are very powerful and
can be very confusing to look at. You should be able to find detailed
descriptions on how to use them in a good JavaScript reference. If
you're interested, try one of Netscape's guides here:

http://devedge.netscape.com/library/...ript/1.3/guide
/regexp.html

Hope that clears things up. Feel free to ask anything else if not.

Hi Michael
Thanks for your expert replies - most appreciated. There is one thing I
would like to ask.
// If no cookie has been set...
if ( !document.cookie.length )
{
// ...show pop-up and set a dummy cookie
document.cookie = 'popup=no';
}

You say in your reply
"The expression evaluates to true if there are no cookies whatsoever
associated with the domain of
your site (document.cookie is zero-length). " Why then would you want to
turn the true to a false by putting an exclamation mark in the if
statement? - by doing that, the lines belonging to the if statement will not
be executed and you do want them to be executed if no cookie has been set.

thanks for your efforts on my behalf
David



Jul 20 '05 #9

P: n/a
David Graham wrote on 21 Nov 2003:
// If no cookie has been set...
if ( !document.cookie.length )
{
// ...show pop-up and set a dummy cookie
document.cookie = 'popup=no';
}

You say in your reply
"The expression evaluates to true if there are no cookies
whatsoever associated with the domain of
your site (document.cookie is zero-length). "
The whole expression "!document.cookie.length" evaluates to true when
there are no cookies. The NOT (!) was part of the expression I was
referring to. Read below for a fuller explanation.
Why then would you want to turn the true to a false by putting an
exclamation mark in the if statement? - by doing that, the lines
belonging to the if statement will not be executed and you do want
them to be executed if no cookie has been set.


If no cookies have been set, the cookie string will be zero-length,
so document.cookie.length returns 0. When evaluated in an if
statement, non-zero values are treated as true and zero as false. So,
without the logical NOT (!), the contents of the if statement would
be skipped when there are no cookies (when the user hasn't visited
the site).

Does that make more sense? Here's another way of looking at it, if
not:

If there are cookies, document.cookie.length > 0
If there are no cookies, document.cookie.length = 0

if( document.cookie.length )
{
// document.cookie.length > 0 - cookies
}
else
{
// document.cookie.length = 0 - no cookies
}

Because we want the pop-up to display when no cookies have been set
(when the user hasn't visited the site), our code would go in the
'else' block above. However, that would mean that the 'if' block
would be empty and that's a waste. By applying NOT (!), we get:

if( !document.cookie.length )
{
// document.cookie.length = 0 - no cookies
}
else
{
// document.cookie.length > 0 - cookies
}

Now the code can go in the 'if' block (what I wrote originally), and
we can remove the 'else' block entirely.

As I said in my last post, the second method (regular expression
matching) I used is more reliable. Anyway, I hope that this makes my
code logic clearer (I almost confused myself when making this
explanation).

Good luck,
Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #10

P: n/a
As I said in my last post, the second method (regular expression
matching) I used is more reliable. Anyway, I hope that this makes my
code logic clearer (I almost confused myself when making this
explanation).

Thanks Michael
I think you explain things very well - if you can clear things up like that
so that someone such as myself can understand it, then believe me, your very
good at relaying info.

Perhaps I should start a new thread with this next problem, but it is
vaguely related, and since I have got you reading this thread can I just add
one more thing. It occurs to me, that if a user hits his back button in the
browser, and returns to the page that set the cookie, things might not go as
sweetly as hoped. Let me explain. From what I have learnt so far about
cookies, they are not visible immediately - they don't become useable until
the browser requests the same page again from the server (assuming the
cookie is still within its expire time). So, the cookie detection methods
outlined so far will not prevent the popup from poping up again if the user
accesses the page from the browsers cache. The solution as I see it, would
be to put in a meta tag a no cache rule so that the page has to be
downloaded every time - however, this is far from ideal. I would be grateful
if you could give me your thoughts on this.
David
Jul 20 '05 #11

P: n/a
David Graham wrote on 22 Nov 2003:

<snip>
Perhaps I should start a new thread with this next problem, but
it is vaguely related, and since I have got you reading this
thread can I just add one more thing. It occurs to me, that if a
user hits his back button in the browser, and returns to the
page that set the cookie, things might not go as sweetly as
hoped. Let me explain. From what I have learnt so far about
cookies, they are not visible immediately - they don't become
useable until the browser requests the same page again from the
server (assuming the cookie is still within its expire time).
So, the cookie detection methods outlined so far will not
prevent the popup from poping up again if the user accesses the
page from the browsers cache. The solution as I see it, would
be to put in a meta tag a no cache rule so that the page has to
be downloaded every time - however, this is far from ideal. I
would be grateful if you could give me your thoughts on this.


As you've no doubt read, 'Set-Cookie' HTTP headers are tranmitted
from server to client whenever the server, or server-side script,
sets a cookie value. The 'Cookie' HTTP header is sent from client to
server on every subsequent request (under the proviso that the new
URI should have access to the cookie). This would mean that any
server-side page must be re-requested for it to get any cookie data.
However, JavaScript runs client-side, where the cookie data is
stored. Whenever a script uses document.cookie, it simply asks the
browser for any cookies associated with the current page and it will
get them immediately.

You'll have no problem here: when I tested the code I listed, I used
both a backward link and my 'Back' button in both IE and Opera, and
neither displayed the pop-up a second time (an alert in my case). It
wasn't until I shut them down that the pop-up showed again.

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #12

P: n/a

"Michael Winter" <M.******@blueyonder.co.uk.invalid> wrote in message
news:Xn*******************************@193.38.113. 46...
David Graham wrote on 22 Nov 2003:

<snip>
Perhaps I should start a new thread with this next problem, but
it is vaguely related, and since I have got you reading this
thread can I just add one more thing. It occurs to me, that if a
user hits his back button in the browser, and returns to the
page that set the cookie, things might not go as sweetly as
hoped. Let me explain. From what I have learnt so far about
cookies, they are not visible immediately - they don't become
useable until the browser requests the same page again from the
server (assuming the cookie is still within its expire time).
So, the cookie detection methods outlined so far will not
prevent the popup from poping up again if the user accesses the
page from the browsers cache. The solution as I see it, would
be to put in a meta tag a no cache rule so that the page has to
be downloaded every time - however, this is far from ideal. I
would be grateful if you could give me your thoughts on this.


As you've no doubt read, 'Set-Cookie' HTTP headers are tranmitted
from server to client whenever the server, or server-side script,
sets a cookie value. The 'Cookie' HTTP header is sent from client to
server on every subsequent request (under the proviso that the new
URI should have access to the cookie). This would mean that any
server-side page must be re-requested for it to get any cookie data.
However, JavaScript runs client-side, where the cookie data is
stored. Whenever a script uses document.cookie, it simply asks the
browser for any cookies associated with the current page and it will
get them immediately.

Thanks Mike
I am getting confused because I am trying to take on board the best of the
ideas in this group - which of course is client side javascript, whilst also
learning the merits of sever side stuff over at alt.php
The only problem that I see with client side code is the fact that 10 - 15%
of users have it disabled (cannot quote source of that but I've seen it
stated many time over at alt.html - they really hate javascript over there.
I am quite catholic in my tastes and wish to learn the good and bad points
of client side and server side code - I might pick the best of both and
combine!
David
Jul 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.