0

I am trying to show a popup on mobile devices, only when the user has first visited a page on my web site. I tried doing this with document.referrer, similar to this. But it does not appear to work on the production site due to it using SSL.

Should I set a cookie when the visitor first comes to the site? Then check for it on subsequent visits? Any ideas? The end result is I need to set a config variable in the popup's javascript config, so that showonmobile: true or false.

Casey
  • 2,611
  • 6
  • 34
  • 60
  • 1
    *Should I set a cookie when the visitor first comes to the site? Then check for it on subsequent visits?* <-- Have you tried that? – Scott Marcus Apr 24 '20 at 22:08
  • https://stackoverflow.com/questions/3528324/how-to-get-the-previous-url-in-javascript – Souleste Apr 24 '20 at 22:10
  • Relying on `referer` doesn't seem to make much sense here. Why can't someone visit your site for second time with the same `referer` as the first time? – Robo Robok Apr 24 '20 at 22:11

1 Answers1

0

I ended up using a cookie to solve this and it is working well:

function setCookie(key, value, expiry) {
    var expires = new Date();
    expires.setTime(expires.getTime() + (expiry * 60 * 60 * 1000));
    document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}

function getCookie(key) {
    var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
    return keyValue ? keyValue[2] : null;
}

function onSiteAlready() {
    if (getCookie('onSite') == 'true') {
        return true;
    }
    else {
        setCookie('onSite', 'true', 1)
        return false;
    }
}

// popup code
showOnMobile: onSiteAlready()
Casey
  • 2,611
  • 6
  • 34
  • 60