0

I have a very simple popup, I would like to set a cookie, so when the visitor visits the page / category for the first time he will be able to see the popup and if he navigates out of the page and comes back, the popup won't appear until he ends the session (closes the browser). Thanks!

<script type="text/javascript">
var link;
var element;
t = setTimeout(openPopUp, 3000);

function openPopUp() {
element = document.getElementById("background");
element.style.display = "block";
element = document.getElementById("popup");
element.style.display = "block";
}

function closePopUp() {
element = document.getElementById("popup");
element.style.display = "none";
element = document.getElementById("background");
element.style.display = "none";
}
</script>

Edit: From comments:

var createCookie = Openpopup(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (60 * 1000));
        expires = ";
        expires = " + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + ";
    path=/";
} 
crv
  • 31
  • 4
  • 10
  • 1
    possible duplicate of [How do I create and read a value from cookie?](http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) – Sverri M. Olsen Sep 23 '14 at 04:44
  • I added the code: I added it for 1 minute to check if it works, but didn't. What am I doing wrong? var createCookie = Openpopup(name, value, days) { var expires; if (days) { var date = new Date(); date.setTime(date.getTime() + (60 * 1000)); expires = "; expires=" + date.toGMTString(); } else { expires = ""; } document.cookie = name + "=" + value + expires + "; path=/"; } – crv Sep 23 '14 at 05:14
  • 2
    You should read a few Javascript tutorials. Come back when you have a basic understanding of what you are doing. – Sverri M. Olsen Sep 23 '14 at 06:52

1 Answers1

1

Something in the like (untested code):

var link;

// ....


if(getCookie("was_here_before")=="") {
    setTimeout(openPopUp, 3000);
    createCookie("was_here_before", "yes", 1);
}

// ....



// these are from SO answer in first comment by Sverri M. Olsen
// [ http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie ]



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

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}


// ....


function openPopUp(url) {
    var element;
    link = url; // don't know what this is for
    element = document.getElementById("background");
    element.style.display = "block";
    element = document.getElementById("popup");
    element.style.display = "block";
}

function closePopUp() {
    var element;
    element = document.getElementById("popup");
    element.style.display = "none";
    element = document.getElementById("background");
    element.style.display = "none";
}
FrancescoMM
  • 2,845
  • 1
  • 18
  • 29
  • Awesome! its works great. Thanks for taking your time to explain. It means a lot.I did test it with one minute ( * 60 * 1000), works as intended. My question is - Is there a way to set cookie for one browser session. The popup has to appear only once per browser session and if the visitor close it and reopen the window, it has to appear again. Thanks FrancescoMM, Again, Thank You Sir! – crv Sep 23 '14 at 14:02
  • @crv have you tried omitting tha days parameter? `createCookie("was_here_before", "yes");` apparently this should set a session cookie – FrancescoMM Sep 23 '14 at 14:08
  • 1
    cool! works like a magic, just removed 1 out of the parameter and when session closes and reopens, popup works. Thanks! – crv Sep 23 '14 at 19:50
  • Found a conflict in code. I installed this code in a popup. Popup suppose to pop on every page. Popup codes have been written in individual pages, but when I apply this code, popup appears only in the page I type in after I close the browser, i.e. If I go to my home page, popup appear once and when I browse other pages no popup in any of the pages. The code is been inserted in each page. I want the popup to appear in all pages only once per session. Any help ? Thanks – crv Sep 24 '14 at 21:09