1

I have fancybox loading on page load on http://cobosboutique.com. I want to add a cookie that only lets it load every 7 days. I found this answer https://stackoverflow.com/questions/23431862/fancybox-popup-once-time-for-session#= but when I use it I get an error that says the Content cannot be loaded. I tried commenting on that thread but I don't have enough points to do that. Does anyone have any ideas what is causing this? I've removed the code for the cookie for now. But this is the code.

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="../template/js/jquery.cookie.js"></script>
<link rel="stylesheet" href="../template/fancybox/source/jquery.fancybox.css?v=2.1.5" type="text/css" media="screen" />
<script type="text/javascript" src="../template/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
<link rel="stylesheet" href="../template/fancybox/source/helpers/jquery.fancybox-thumbs.css?v=1.0.7" type="text/css" media="screen" />
<script type="text/javascript" src="../template/fancybox/source/helpers/jquery.fancybox-thumbs.js?v=1.0.7"></script>
<script type="text/javascript">
    function openFancybox() {
    setTimeout(function () {
        $('#yt').trigger('click');
    }, 500);
};
</script>
<script type="text/javascript">
    $(document).ready(function () {
    var visited = $.cookie('visited');
    if (visited == 'yes') {
        return false; // second page load, cookie active
    } else {
        openFancybox(); // first page load, launch fancybox
    }
    $.cookie('visited', 'yes', {
        expires: 0 // the number of days cookie  will be effective
    });
    $("#yt").click(function () {
        $.fancybox({
            href: this.href,
            type: "inline"
        });
        return false;
    });
});
</script>
<a class="fancybox" href="#yt">test</a>
    <div id="yt" style="display:none; text-align: center;"><h2>VIP TEXT CLUB!</h2>
<h3>Get exclusive access to sales and new arrivals!</h3>
<h3>Text "COBO" to 51660 and receive 10% off your entire purchase! </h3>
<h3>10% offer only applies to first time text club subscribers.</h3></div>
Community
  • 1
  • 1
Tell Stevens
  • 149
  • 1
  • 4
  • 16

1 Answers1

1

Bear in mind that #yt is the ID of your target content (your hidden div that supposed to be in fancybox) therefore you cannot bind and trigger a click event to that selector but the one with class fancybox (the trigger element), which is actually pointing to the #yt selector :

<a class="fancybox" href="#yt">test</a>

So, in your openFancybox() function you need to replace this line

$('#yt').trigger('click');

... by this one

$('.fancybox').trigger('click');

Then bind the click event to .fancybox replacing this line

$("#yt").click(function () {

... by this one

$(".fancybox").click(function () {

See JSFIDDLE

JFK
  • 40,963
  • 31
  • 133
  • 306