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

Discussion in 'Javascript' started by chrism, Nov 19, 2003.

  1. chrism

    chrism Guest

    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
     
    chrism, Nov 19, 2003
    #1
    1. Advertising

  2. chrism

    kaeli Guest

    In article <>,
    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
     
    kaeli, Nov 19, 2003
    #2
    1. Advertising

  3. chrism

    chrism Guest

    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
     
    chrism, Nov 19, 2003
    #3
  4. 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
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 19, 2003
    #4
  5. chrism

    David Graham Guest

    "Michael Winter" <> wrote in message
    news:Xns9438EE643F19DMWinterBlueyonder@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
    > d (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
     
    David Graham, Nov 20, 2003
    #5
  6. chrism

    kaeli Guest

    In article <>,
    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
     
    kaeli, Nov 20, 2003
    #6
  7. 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/manuals/2000/javascript/1.3/guide
    /regexp.html

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

    Mike

    --
    Michael Winter
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 20, 2003
    #7
  8. JRS: In article <>, seen in
    news:comp.lang.javascript, kaeli <> 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.
     
    Dr John Stockton, Nov 20, 2003
    #8
  9. chrism

    David Graham Guest

    "Michael Winter" <> wrote in message
    news:Xns943998947850AMWinterBlueyonder@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/manuals/2000/javascript/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
     
    David Graham, Nov 21, 2003
    #9
  10. 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
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 22, 2003
    #10
  11. chrism

    David Graham Guest


    > 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
     
    David Graham, Nov 22, 2003
    #11
  12. 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
    d (remove ".invalid" to reply)
     
    Michael Winter, Nov 22, 2003
    #12
  13. chrism

    David Graham Guest

    "Michael Winter" <> wrote in message
    news:Xns943B7D2DB5853MWinterBlueyonder@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
     
    David Graham, Nov 22, 2003
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    5,862
    Steven Cheng[MSFT]
    Jun 3, 2004
  2. Replies:
    2
    Views:
    8,533
    Randy Webb
    Feb 4, 2006
  3. Techsatish
    Replies:
    0
    Views:
    462
    Techsatish
    Aug 25, 2006
  4. Replies:
    2
    Views:
    144
    Randy Webb
    Feb 4, 2006
  5. sixstringsk

    Show GreyBox Pop Up Only Once?

    sixstringsk, May 27, 2008, in forum: Javascript
    Replies:
    3
    Views:
    145
    Peter Michaux
    May 28, 2008
Loading...

Share This Page