0

I'm trying to reload the page only when the cookie (userInfo) value includes pageCount=1.

This is my cookie value for userInfo:

lastBranchVisitedName=No branch set&mostRecentBranchId=&pageCount=4&lastPageUrl=/personal/life-health/over-50s-insurance/&allocatedBranchId=70&allocatedBranchTelephone=01993 894 700&allocatedBranchName=Motor Direct&locationLatitude=51.8969248&locationLongitude=-2.0758525999999997&lastPageDateTime=28/03/2017 14:37:26

What i am trying to achieve is that once the geoLocation is allowed, if the pageCount=1 then reload the page once. Currently the page is reloading regardless of what pageCount=.

Here is my current Javascript:

// Hide / show notification depending on cookie
window.onload = function () {

if (setCookieValue("geoPopUpCompleted") == undefined) {

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(setPosition);

    } else {
        alert("Unfortunately we're unable to find your location");
    }

}

// Get page count
var pageCount = getCookie("pageCount");
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 setPosition(position) {
    setCookieValue("locationLongitude", position.coords.longitude);
    setCookieValue("locationLatitude", position.coords.latitude);
    setCookieValue("geoPopUpCompleted", true);

    if (pageCount) {
        (function () {
            if (window.localStorage) {
                if (!localStorage.getItem("firstLoad")) {
                    localStorage["firstLoad"] = true;
                    window.location.reload();
                }
                else
                    localStorage.removeItem("firstLoad");
            }
        })();
   }
}
}

// GEO cookie settings
function setCookieValue(cookieName, cookieValue) {
    var cookieExpireDate = "expires=Sun, 31 Dec 4017 12:00:00 UTC; path=/";
    document.cookie = cookieName + "=" + cookieValue + "; " + cookieExpireDate;
}

Not looking for JQuery solutions.

Thanks for your help

DBoi
  • 627
  • 2
  • 17
  • 35
  • Your code apparently lacks a cookie getter function – hindmost Mar 28 '17 at 13:55
  • Do you have an example of how i might do this? – DBoi Mar 28 '17 at 14:01
  • You could easily find examples yourself: [1](http://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript), [2](http://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript) and so on. – hindmost Mar 28 '17 at 14:10
  • 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) – hindmost Mar 28 '17 at 14:10
  • Have updated my code to what i was trying previously but was again unable to get it to work correctly – DBoi Mar 28 '17 at 14:15

1 Answers1

0

The following code that worked for me... hope this helps someone else.

// Hide / show notification depending on cookie
window.onload = function () {

if (setCookieValue("geoPopUpCompleted") == undefined) {

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(setPosition);

    } else {
        alert("Unfortunately we're unable to find your location");
    }

}

function setPosition(position) {
    setCookieValue("locationLongitude", position.coords.longitude);
    setCookieValue("locationLatitude", position.coords.latitude);
    setCookieValue("geoPopUpCompleted", true);

    // Get page count
    var pageCount = getCookie("pageCount");
    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=1");
            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 "";
    }

    // Reload page once
    if (pageCount) {
        (function () {
            if (window.localStorage) {
                if (!localStorage.getItem("firstLoad")) {
                    localStorage["firstLoad"] = true;
                    window.location.reload();
                }
                else
                    localStorage.removeItem("firstLoad");
            }
        })();
    }
}
}

// GEO cookie settings
function setCookieValue(cookieName, cookieValue) {
var cookieExpireDate = "expires=Sun, 31 Dec 4017 12:00:00 UTC; path=/";
document.cookie = cookieName + "=" + cookieValue + "; " + cookieExpireDate;
}
DBoi
  • 627
  • 2
  • 17
  • 35