If cookie causing page to be busy

Hi,
I am just starting out with javascript so may be doing something really stupid so please forgive my ignorance. I am trying to build a slide up advert which only shows if a cookie has not been set to hide it. I have some code for the slider that works but i want to add to that code to do the If cookie is set don’t show part.

this is what i have

 <script type="text/javascript">
$(document).ready(function(){
    if(readCookie("offer") !==''){
        
            $.ajaxSetup({cache:false});
            $('#triqui_ad').wrap('<div id="triqui_container"></div>');
            $('#triqui_ad').load('ajax.php',function(){
                $('#triqui_ad').css('display','block');
                $('#triqui_container').hide().delay( 900 ).slideDown('slow');
                $('#triqui_ad').append(' &bull; <a id="triqui_ad_close">Close</a> ');
                $('#triqui_ad_close').click(function(){ 
                    $('#triqui_container').slideUp('slow'); 
                }); 
            }); 
    }
    else{  
        document.write('the cookie says no')
    };
});

At the moment it works if the cookie is set to empty but if i put a value in then the page loading icon in the browser continues to try and load something. I assume it is not finishing something that it should. I can’t see what i need to change to end it as to me it looks like it should be ok as if the cookie is empty or not set the slider runs and if it is set and has a value then it should just print out ‘the cookie says no’.

what am i missing?

I’m using these functions ot set and unset things http://www.sitepoint.com/how-to-deal-with-cookies-in-javascript/

Hi Noppy,

First off, don’t use document.write. It’s a bad practice. Either use console.log if you’re just debugging, otherwise create an element and append it to something, e.g.

$("body").append("<p>The cookie says no</p>");

That said, here’s how I’d do things. I’m using the js.cookie plugin for ease of use

<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8" />
    <title>Popup demo</title>
  </head>
  <body>

    <script src="js.cookie.js"></script>
    <script>
      var hasSeenPopup =  Cookies.get('popup');
      if(!hasSeenPopup){
        // Code to show the popup here
        Cookies.set('popup', 'hello');
      } else {
        console.log("The popup has already been shown.");
      }
    </script>
  </body>
</html>

thanks for that. Using that as a base i managed to get something working. It’s probably a bit messy but seems to do the job and i’ve learnt a fair bit about functions and js cookies. I had some code already for the cookie side of things that i kind of understood so i kept with that rather than js.cookie.

thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.