473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
12 17927
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
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
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
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

"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
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
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

"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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jaakko T Oksa | last post by:
The following script sets,resets or displays the contents of a cookie. When I place the script in a directory under the iis root, let's say "mysite" (http://localhost/mysite/tst.asp) it works as...
1
by: Rahul Chatterjee | last post by:
Hello All I have an asp page problem (the nature is slightly complex so please bear with the long winded explanation). We have a sandbox front end domain and a sandbox backend domain. The...
2
by: Michael | last post by:
I am reading and setting a cookie using JavaScript in the BODY onload and onunload events respectively. This works fine. However when I use ASP to set the cookie under some condition where I...
1
by: sun | last post by:
In Javascript, I write to document.cookie, and retrieve it in server side. It's in a treenode control, when the node is expanded, I write document.cookie: if(subTree.style.display ==...
4
by: MrL8Knight | last post by:
Hello, I am trying to build a simple php form based shopping cart using a cookie with arrays. I need to use 1 cookie because each order will have over 20 items. With that said, I realize I need to...
6
by: Victor | last post by:
Hi everybody, could anybody help me with the following problem : I need to set a cookie containing a Russian character string as the value, using the construct "document.cookie = ...". The...
19
by: 848lu | last post by:
hi, i wanted to display cookie info on a form, as in a text box ok this is what im doing, im saving all info on a cookie where the users types in i.e. card details. and at the end i made a...
3
by: StevenT | last post by:
Hello, I am trying to dynamically create a table based on the information I have in my cookie for a shopping cart. I can create it and display it and all is good. I put the contents of the...
3
by: rjoseph | last post by:
Hi Guys I hope this is an easy one for you. Basically I have a page on my website that creates a javascript based cookie on the users pc. The contents of the cookie when created looks as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.