We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29703
    • 217 Posts
    Hello,

    I figured I’d stop off here to try and offload my dilemma in hope of solving this confounded problem.

    Firstly, I have no javascript ability.

    Now, I am trying to create a DIV popup which pops up for first time viewers and when closed it doesn’t pop up again for that user. I am currently using a piece of javascript that creates a cookie when the ’close div popup’ button is pressed. When the page is loaded it looks as to whether the cookie is there; if it is, it doesn’t display the popup.

    However it isn’t working as planned. The cookie is being created, but the popup is not disappearing.

    I have a live example here:

    http://www.henryhoffman.com/newsite/

    If someone can have a look at the code and give me a quick rundown of my errors, it would be greatly appreciated

    I’ve been staring at this for four hours and my mind is becoming numb.
      • 29703
      • 217 Posts
      I will be using conditional comments, this is just a test to get the cookies working. I should have probably made that clearer in my previous post.

      Thanks for the Sitepoint link, I was aware that they used Javascript for the Popups and shall look into it. What benefits would I have for using PHP to set the cookies?

      Thanks,

      Henry

      I’m really trying to work this out in my head, but can’t get the hang of it. Is the following code completely wrong as this is the conclusion I’ve arrived at but can’t seem to get work:

      <script type="text/javascript">
      if (getCookie("here") != "yes") document.getElementById('iebox').style.display = "block";
      else SetCookie('here','yes',new Date(new Date().getTime()+(1000*60*60*24*10)));
      </script>
        • 24719
        • 194 Posts
        Quote from: thehen at Mar 09, 2008, 05:46 AM

        <script type="text/javascript">
        if (getCookie("here") != "yes") document.getElementById('iebox').style.display = "block";
        else SetCookie('here','yes',new Date(new Date().getTime()+(1000*60*60*24*10)));
        </script>


        you’re never setting a cookie with this code. you’re saying:

        if the user doesn’t have a cookie set, show them the popup,
        if they do have the cookie set, set a cookie.

        you should instead do:

        <script type="text/javascript">
        if (getCookie("here") != "yes") 
        {
          document.getElementById('iebox').style.display = "block";
          SetCookie('here','yes',new Date(new Date().getTime()+(1000*60*60*24*10)));
        }
        </script>
          • 29703
          • 217 Posts
          Thanks for the point in the right direction, it’s greatly appreciated.

          Isn’t this saying that if the cookie exists to display ’iebox’ as a block and to create a cookie?

          How would I go about changing it so that if the cookie doesn’t exist to display ’iebox’ as a block and to create a cookie?

          It’s not just != "no" instead of != "yes" is it?

          Thanks
            • 24719
            • 194 Posts
            i’m assuming you’ve copied getcookie and setcookie from:

            http://www.w3schools.com/js/js_cookies.asp

            because they aren’t standard js functions?

            getcookie will return an empy string if the cookie doesn’t exist. so the following code:

            <script type="text/javascript">
            if (getCookie("here") != "yes") 
            {
              document.getElementById('iebox').style.display = "block";
              SetCookie('here','yes',new Date(new Date().getTime()+(1000*60*60*24*10)));
            }
            </script>
            


            is saying:

            if the cookie ’here’ has any value other than ’yes’, show ’iebox’, and then set a cookie so that ’here’ = ’yes’.

            the next time the user visits, they will have a cookie ’here’ that equals ’yes’, so ’iebox’ won’t be displayed (assuming it’s default css sets ’display:none’).
              • 29703
              • 217 Posts
              Quote from: redman at Mar 09, 2008, 07:40 AM

              i’m assuming you’ve copied getcookie and setcookie from:

              http://www.w3schools.com/js/js_cookies.asp

              because they aren’t standard js functions?

              getcookie will return an empy string if the cookie doesn’t exist. so the following code:

              <script type="text/javascript">
              if (getCookie("here") != "yes") 
              {
                document.getElementById('iebox').style.display = "block";
                SetCookie('here','yes',new Date(new Date().getTime()+(1000*60*60*24*10)));
              }
              </script>
              


              is saying:

              if the cookie ’here’ has any value other than ’yes’, show ’iebox’, and then set a cookie so that ’here’ = ’yes’.

              the next time the user visits, they will have a cookie ’here’ that equals ’yes’, so ’iebox’ won’t be displayed (assuming it’s default css sets ’display:none’).

              Okay this is where my lack of javascript knowledge is becomming embarrassing embarrassed

              Firtsly I did think they were standard JS functions for some reason. . .

              I have tried incorporating the W3schools into my page, but I’m throughly buried in ignorance now.

              I have the page live at www.henryhoffman.com/newsite

              If you could take a look Redman it would be much appreciated.
                • 29703
                • 217 Posts
                I’ve fixed the error.

                The cookies are being created and everything’s working now, apart from the cookie detection seemingly.

                if (getCookie("here") != "yes") seems to be processing even if the cookie is there or not.

                Any ideas?

                Live example: www.henryhoffman.com/newsite

                Update: It’s working on Firefox but not IE huh
                  • 24719
                  • 194 Posts
                  try changing setcookie to:

                    setCookie('here','yes',10);


                  where 10 is the number of days to set a cookie for. this works for me.
                    • 29703
                    • 217 Posts
                    Quote from: redman at Mar 09, 2008, 08:48 AM

                    try changing setcookie to:

                      setCookie('here','yes',10);


                    where 10 is the number of days to set a cookie for. this works for me.

                    YIPPEEEE!

                    Thank you implicitly Redman!

                    I can finally get back to designing, phew. My brain is mush after that battle.