-1

I am new to javascript and jQuery. I am using SimpleModal basic to display a popup onload when a user visits my site. It's working great, but each time visitors go to homepage, the popup displays.

For example:

  • Visitor A goes to my homepage, the popup displays for the first time - it's great
  • Visitor A goes to a page inside my website, and click on logo to come back to homepage, the popup displays for the second time, and keep showing each time visitor A back to homepage - it's not good.

I need this popup to be displayed only once per session.

I have searched all over the internet, and have seen that this can be taken care of using a cookie, but I'm not sure of how to tie the cookie function into the function of the modal script.

How can I implement a cookie to track the modal function, and display it only once when the page loads?

Here is the script that loads the modal content:

<?php if($_SERVER['REQUEST_URI'] == '/'): ?>


<script type="text/javascript" src="<?php bloginfo(template_url);?>/assets/javascripts/jquery.leanModal.min.js"></script>

<script type="text/javascript">
    jQuery(document).ready(function() { 
        jQuery('#basic-modal-content').modal();

    });
</script>
<?php endif;?>
nathanchere
  • 8,008
  • 15
  • 65
  • 86
Tony Nguyen
  • 1
  • 1
  • 1
  • possible duplicate of [PHP: How do Cookies and Sessions work?](http://stackoverflow.com/questions/11142882/php-how-do-cookies-and-sessions-work) - Please consult the PHP manual and the internet reference standard and comments documentation about the features you want to use (HTTP State mechanism - http://tools.ietf.org/html/rfc6265). This is normally already documented and a bit much to explain. – hakre Sep 25 '13 at 05:25

1 Answers1

0

you can do this using a jquery plugin called jQuery cookie, having downloaded it and included it on the page using script tags; change your code to below

<script type="text/javascript">
    jQuery(document).ready(function() { 
        if (jQuery.cookie('shown_modal')==undefined) {
           jQuery.cookie('shown_modal', 'true');
           jQuery('#basic-modal-content').modal();
        }
    });
</script>
Yussuf S
  • 2,022
  • 1
  • 14
  • 12
  • If you think that is the answer, please link in a comment (or vote to close if you've obtained the needed reputation) to the duplicate Q&A material where the question is more properly asked. This question is actually unrelated to simplemodal but about using cookies across HTTP requests and how jquery offers an API for it. See [How to use cookie , JQuery, Javascript?](http://stackoverflow.com/a/12625495/367456) – hakre Sep 25 '13 at 05:44