473,895 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18027
In article <b8************ *************@p osting.google.c om>,
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.cooki e.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.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #4
In article <b8************ *************@p osting.google.c om>,
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(cooki eName, 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.getDat e() + expireDate);
expires = expires.toGMTSt ring();
}
document.cookie = cookieName+"="+ cookieValue+";e xpires="+expire s;
}

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

function getCookie (cookieName)
{
cookieValue = ""
if (document.cooki e.indexOf(cooki eName) == -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(cookie Name);
// get the beginning index of the cookie value by looking for the
equal sign after the name
cookieValStart = (document.cooki e.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(cook ieValStart,
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.******@bluey onder.co.uk.inv alid> 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.cooki e.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.******@blueyo nder.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.cooki e.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.cooki e.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.cooki e 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.toGMTStr ing()))
}
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.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #7
JRS: In article <MP************ ************@nn tp.lucent.com>, seen in
news:comp.lang. javascript, kaeli <ti******@NOSPA M.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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8

"Michael Winter" <M.******@bluey onder.co.uk.inv alid> 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.cooki e.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.cooki e 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.toGMTStr ing()))
}
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.cooki e.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.cooki e 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.cooki e.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.cooki e is zero-length). "
The whole expression "!document.cook ie.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.cooki e.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.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1850
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 expected. When I create a virtual directory "mysite" and point it to the directory where the script is, the cookie can no longer be set after it has been cleared, even though it is accessed in the same location (http://localhost/mysite/tst.asp)
1
793
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 front end communicates with the back end using XML data interchange and URL variables. THe website has frames - the left frame consisting of the navigation and the right frame consisting of the displays of information. We have implemented a search...
2
5604
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 want to override the last value set in the onunload event, it has no effect. The next onload still has the old value. Any ideas? The following code simulates the problem I experience. --------------------------------------------------
1
2033
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 == 'inline') { icon.src = icon.CollaspedSrc; e.src = e.CollaspedSrc;
4
7175
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 serialize the data to put the array into the cookie. That part of my code is working just fine and displaying fine. The problem I’m having is when I try to unserialize and display; the data does not appear. If I remove my unserialize command line (see...
6
4376
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 whole project runs in the UTF-8 encoding. The Internet Explorer handles this cookie correctly, whereas the FireFox writes only some byte gargabe and then reads it back.
19
2205
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 confirmation page, where all the info they typed in are here. so obviously we use cookie, but i wanted to display it in a text box. how do i do that...
3
1993
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 cookie into a multidimensional array and loop through the array creating rows and cells based on the information. I create TextBox's and add them to the cells for displaying the quantity. So I want to display the quantity of an item in a TextBox and...
3
1793
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 follows: ------------------------------ DS1113008PT_ToDoItem1 My%20message%20goes%20here www.mydomain.co.uk/carsearch/usedcars/
0
9990
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11249
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10845
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10935
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9649
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8028
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5863
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4689
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.