Robinho
Robinho

Reputation: 1

Add cookie for newsletter-modal-popup

I am a beginner in asp.net and I want to make a newsletter pop up for my website

The pop-up has been made successfully, and it is shown at the bottom right of the website. Now I want to add a cookie, so when visitors click on the 'sign up'-button or on the close button, the pop up is (never) shown again.

But here I am stuck, and don't know how to begin to add a cookie...

This is my code:

<!-- Popup newsletter start -->

    <div class="dialog" title="Save time, save money!">
        <form>
            <p>Sign up and we'll send you the best deals</p>
                <input type="email" id="email" name="email" value ="Enter your e-mail address here">
                <input type="submit" value="Sign up">
        </form>
    </div>

    <script type="text/javascript">
        jQuery(document).ready(function () {
        jQuery(".dialog").dialog({
            bgiframe: true, autoOpen: true, height: '150', width: '350', modal: false, 
            position: {
                my: "right bottom",
                at: "right bottom",
            },
            create: function (event) {
                $(event.target).parent().css({ 'position': 'fixed'});
            }
        });
    });
    </script>


<!-- Popup newsletter end -->

Picture newsletter-popup

Upvotes: 0

Views: 1274

Answers (1)

ecolema
ecolema

Reputation: 598

jQuery(document).ready(function () {
    if(getCookie('popup_shown') == null) {
        jQuery(".dialog").dialog({
            bgiframe: true, autoOpen: true, height: '150', width: '350', modal: false, 
            position: {
                my: "right bottom",
                at: "right bottom",
            },
            create: function (event) {
                $(event.target).parent().css({ 'position': 'fixed'});
            }
        });
    }
});

jQuery('.close-button').on('click', setPopup_shown );
jQuery('form').on('submit', setPopup_shown);

function setPopup_shown() {
    setCookie('popup_shown', 'true');
}

function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

Upvotes: 0

Related Questions