1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to show something only once during a browser session???

Discussion in 'JavaScript' started by netpox, Oct 7, 2009.

  1. #1
    Here is the example: http://www.icyhats.com/info.php

    Here is the code i am using:
    SEMrush
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <title>Sandbox</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <link href="http://www.icyhats.com/stylesheet.css" rel="stylesheet" type="text/css">
     
    </head>
    <body>
    <div id='message' style="display: none;">
        <span>Hey, This is my Message.</span>
        <a href="#" class="close-notify" onclick="closeNotice()">X</a>
    </div>
    <script type="text/javascript"> 
    <!-- we run in the footer so no need to use onload -->
    $(document).ready(function() {
        $("#message").fadeIn("slow");
    });
     
    function closeNotice() {
        $("#message").fadeOut("slow");
    }
    </script>
    
    </body>
    </html>
    
    
    
    PHP:

    I want it to only show this once per session...if user clicks on the X it shouldnt show anymore if they browse through the site. So basically if they click on X and i refresh the page it shouldnt show up again. Anyone know how to do this?
     
    Last edited: Oct 7, 2009
    netpox, Oct 7, 2009 IP
    SEMrush
  2. dnlinxor

    dnlinxor Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i dont know much about it, but try implementing with a cookie, if a cookie contains certain information, then dont run a part of your script. a good place to learn about cookies, if you decide that you need it ==> w3schools(dot)com javascript => cookies
     
    Last edited: Oct 8, 2009
    dnlinxor, Oct 8, 2009 IP
  3. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Yea i know its with a cookie but dont know what code to use...anyone know?
     
    netpox, Oct 8, 2009 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    You could do this easily server-side.. Just use a session variable. For example, with ASP.
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <title>Sandbox</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <link href="http://www.icyhats.com/stylesheet.css" rel="stylesheet" type="text/css">
     
    </head>
    <body>
    <div id='message' style="display: none;">
        <span>Hey, This is my Message.</span>
        <a href="#" class="close-notify" onclick="closeNotice()">X</a>
    </div>
    <% If Session("shown") = "" Then %>
    <script type="text/javascript"> 
    <!-- we run in the footer so no need to use onload -->
    $(document).ready(function() {
        $("#message").fadeIn("slow");
    });
     
    function closeNotice() {
        $("#message").fadeOut("slow");
    }
    </script>
    <% Session("shown") = "yep" 
    End If
    %>
    </body>
    </html>
    
    Code (markup):
     
    camjohnson95, Oct 9, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    or something like that....
     
    camjohnson95, Oct 9, 2009 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    http://fragged.org/dev/oncePerSession.php -> via js/cookies.

    var Cookie = {
        // wrapper for working with cookies.
        set: function(c_name, value, options) {
            options = (typeof(options) === "undefined") ? {
                duration: null, // in days, if null, session only
                path: null // relative to domain, if null, default from URI
            } : options;
    
            if (options.duration !== null) {
                var exdate = new Date();
                exdate.setDate(exdate.getDate()+options.duration);
            }
    
            document.cookie=c_name+ "=" +escape(value)+ ((options.duration===null) ? "" : ";expires="+exdate.toGMTString()) + ((options.path===null) ? "" : ";path="+options.path);
        },
        get: function(c_name) {
            if (document.cookie.length > 0) {
                var c_start = document.cookie.indexOf(c_name + "=");
                if (c_start !== -1) {
                    c_start = c_start + c_name.length+1;
                    var c_end = document.cookie.indexOf(";",c_start);
                    if (c_end === -1)
                        c_end = document.cookie.length;
                    return unescape(document.cookie.substring(c_start,c_end));
                }
            }
            return false;
        },
        remove: function(c_name) {
            this.set(c_name, "", {duration: -1, path: "/"});
        }
    }; // end Cookie wrapper
    
    var shownMessage = Cookie.get("shownMessage");
    
    if (shownMessage === false) {
        // first time.
        alert("one time only message from Derren Brown:\nthis week's winning lottery numbers will be:\n\n9,12,31,34,38,45");
        Cookie.set("shownMessage", "true", {path: "/"}); // no expiry date offset so session only.
    }
    else {
        alert("you have already seen the message\nclose browser to repeat");
        // Cookie.remove("shownMessage"); // uncomment to test with multiple reloads.
    }
    
    PHP:
    p.s. the wrapper for cookies is still a work in progress but it's enough for our purposes here.
    in your case in your ready func, once you add the Cookie wrapper (i'd consider sticking it to jQuery.cookie namespace and then referencing it as $.cookie.set() etc. )
    
    var shownMessage = Cookie.get("shownMessage");
    
    $(document).ready(function() {
        if (shownMessage === false) {
            $("#message").fadeIn("slow");
            Cookie.set("shownMessage", "true", {path: "/"}); // no expiry date offset so session only.
        }
    });
     ...
    
    PHP:
     
    Last edited: Oct 9, 2009
    dimitar christoff, Oct 9, 2009 IP