Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post it once script cookie not working

    I am using JavaScript Kits Post It Note script on my website. The issue that I am having is the cookie to show it only once does not work. It sets as confirmed in the browser but still shows the note on refresh or visiting another page and then returning to that same page where the person closed the post it note. The only thing that I have changed is that I made a PHP page and am using includes to add to page and made the CSS a separate CSS file to include that. I have tried just including it into the page on its own and it still does not work so I don't think the issue is the way I did the includes. Here is the code offered my JavaScript Kit. Can anyone tell me why the cookie is not working. I would appreciate it. I have compared it to similar cookies without the NS4, IE4, and NS6 expressions and other than that looks the same.

    [code]<div id="postit" style="left:150px;top:150px">
    <div align="right"><b><a href="javascript:closeit()">[Close It]</a></b></div>

    <!--INSERT YOUR CONTENT HERE-->

    <b>Reminder:</b><br />
    <p><a href="http://freewarejava.com" target="_new">Looking for
    free Java applets?<br />
    </a>Be sure to check out our sister site Freewarejava.com</p>
    <p><a href="http://www.dynamicdrive.com" target="_new">Looking for
    more scripts?<br />
    </a>Visit our partner Dynamic Drive for free, original DHTML scripts and
    components.</p>

    <!--END YOUR CONTENT HERE-->

    </div>

    <script>

    //Post-it only once per browser session? (0=no, 1=yes)
    //Specifying 0 will cause postit to display every time page is loaded
    var once_per_browser=1

    ///No need to edit beyond here///

    var ns4=document.layers
    var ie4=document.all
    var ns6=document.getElementById&&!document.all

    if (ns4)
    crossobj=document.layers.postit
    else if (ie4||ns6)
    crossobj=ns6? document.getElementById("postit") : document.all.postit


    function closeit(){
    if (ie4||ns6)
    crossobj.style.visibility="hidden"
    else if (ns4)
    crossobj.visibility="hide"
    }

    function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if cookie exists
    offset += search.length
    // set index of beginning of value
    end = document.cookie.indexOf(";", offset);
    // set index of end of cookie value
    if (end == -1)
    end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }

    function showornot(){
    if (get_cookie('postdisplay')==''){
    showit()
    document.cookie="postdisplay=yes"
    }
    }

    function showit(){
    if (ie4||ns6)
    crossobj.style.visibility="visible"
    else if (ns4)
    crossobj.visibility="show"
    }

    if (once_per_browser)
    showornot()
    else
    showit()

    </script>

    <script language="JavaScript1.2">

    //drag drop function for ie4+ and NS6////
    /////////////////////////////////

    function drag_drop(e){
    if (ie4&&dragapproved){
    crossobj.style.left=tempx+event.clientX-offsetx+'px'
    crossobj.style.top=tempy+event.clientY-offsety+'px'
    return false
    }
    else if (ns6&&dragapproved){
    crossobj.style.left=tempx+e.clientX-offsetx+'px'
    crossobj.style.top=tempy+e.clientY-offsety+'px'
    return false
    }
    }

    function initializedrag(e){
    if (ie4&&event.srcElement.id=="postit"||ns6&&e.target.id=="postit"){
    offsetx=ie4? event.clientX : e.clientX
    offsety=ie4? event.clientY : e.clientY

    tempx=parseInt(crossobj.style.left)
    tempy=parseInt(crossobj.style.top)

    dragapproved=true
    document.onmousemove=drag_drop
    }
    }
    document.onmousedown=initializedrag
    document.onmouseup=new Function("dragapproved=false")

    </script>[code]

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    var ns4=document.layers
    var ie4=document.all
    var ns6=document.getElementById&&!document.all

    This is dismally old code. Those browsers have long been one with Nineveh and Tyre. I can only suggest you junk it and use something more up-todate.

    I am sure that we covered using a cookie to display a message once per browser session recently in this forum - have a look at
    http://www.codingforums.com/showthread.php?t=289486

    BTW, when posting here please help us to help you by making it easier to copy, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.


    Quizmaster: What relation was King Edward VIII to the Queen?
    Contestant: Husband.
    Last edited by Philip M; 03-19-2013 at 07:53 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    superzeeman (03-19-2013)

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I didnt see any way to edit the post so here is the script in format you requested:

    Code:
    <script>
    
    //Post-it only once per browser session? (0=no, 1=yes)
    //Specifying 0 will cause postit to display every time page is loaded
    var once_per_browser=1
    
    ///No need to edit beyond here///
    
    var ns4=document.layers
    var ie4=document.all
    var ns6=document.getElementById&&!document.all
    
    if (ns4)
    crossobj=document.layers.postit
    else if (ie4||ns6)
    crossobj=ns6? document.getElementById("postit") : document.all.postit
    
    
    function closeit(){
    if (ie4||ns6)
    crossobj.style.visibility="hidden"
    else if (ns4)
    crossobj.visibility="hide"
    }
    
    function get_cookie(Name) {
      var search = Name + "="
      var returnvalue = "";
      if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        if (offset != -1) { // if cookie exists
          offset += search.length
          // set index of beginning of value
          end = document.cookie.indexOf(";", offset);
          // set index of end of cookie value
          if (end == -1)
             end = document.cookie.length;
          returnvalue=unescape(document.cookie.substring(offset, end))
          }
       }
      return returnvalue;
    }
    
    function showornot(){
    if (get_cookie('postdisplay')==''){
    showit()
    document.cookie="postdisplay=no"
    }
    }
    
    function showit(){
    if (ie4||ns6)
    crossobj.style.visibility="visible"
    else if (ns4)
    crossobj.visibility="show"
    }
    
    if (once_per_browser)
    showornot()
    else
    showit()
    </script>
    
    <script language="JavaScript1.2">

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by superzeeman View Post
    I didnt see any way to edit the post so here is the script in format you requested:
    Did you try using the EDIT button?

    That code is too ancient and obsolete to be worth bothering with. You should junk it and use something more up-to-date.

    I did give you a link to another script which you ought to be able to adapt.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    superzeeman (03-19-2013)

  • #5
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Yea, I know its old but It gave me what I needed except for the cookie issue. Thanks for the link. I will see what I can do with it.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    Here is the code modernized:
    Code:
    <script type="text/javascript">
    //Post-it only once per browser session? (0=no, 1=yes)
    //Specifying 0 will cause postit to display every time page is loaded
    var once_per_browser=1
    var crossobj = document.getElementById("postit");
    
    function closeit()
    {
        crossobj.style.visibility="hidden";
    }
    
    function get_cookie(Name) 
    {
        var search = Name + "=";
        if (document.cookie.length > 0) 
        {
            var offset = document.cookie.indexOf(searcg);
            if (offset != -1) 
            {
                offset += search.length;
                var end = document.cookie.indexOf(";", offset);
                if (end < 0 ) end = document.cookie.length;
                return unescape(document.cookie.substring(offset, end));
          }
       }
       return "";
    }
    
    function showornot()
    {
        if (get_cookie('postdisplay')=='')
        {
            showit();
            document.cookie="postdisplay=no";
        }
    }
    
    function showit()
    {
         crossobj.style.visibility="visible"
    }
    
    if (once_per_browser) showornot();
    else showit();
    </script>
    But I think that code is *WAY* overkill. In addition, I think you should use style.display instead of style.visibility. Reason: If you just change the visibility, the message block *still* takes up the same space on the page, it's just hidden.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    Here, how's this for much simplified code? And very modern, at the same time.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    #postit {
         position: absolute; 
         left: 150px; top: 150px; 
         height: 200px; width: 400px;
         border: solid brown 3px;
         background-color: yellow;
         color: brown;
    }
    #closer {
         position: relative;
         width: 100%; 
         text-align: right; 
         font-weight: bold;
    }
    </style>
    </head>
    <body>
    <div id="postit"> 
        <div id="closer">[Close It]</div>
        <b>Reminder:</b><br />
        <p>
            <a href="http://freewarejava.com" target="_new">
                Looking for free Java applets?</a>
            <br />
            Be sure to check out our sister site Freewarejava.com
        </p>
    </div>
    <div>
       ... your main page content here ...
    </div>
    
    <script type="text/javascript">
    (
      function( )
      {
          var once_per_browser = true;
    
          var divPostit = document.getElementById("postit");
          var divCloser = document.getElementById("closer");
          
          divCloser.onclick = function() { divPostit.style.display = "none"; };
    
          if ( ! once_per_browser ) { return; /* nothing else to do */ }
    
          if ( document.cookie.indexOf("ShowPostitOnceOnly") >= 0 )
          {
              divPostit.style.display = "none";
          }
          document.cookie = "ShowPostitOnceOnly=whatever";
      }
    )();
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •