从cookie字符串javascript获取cookie值

时间:2017-03-28 13:49:52

标签: javascript cookies

我只是在cookie(userInfo)值包含pageCount = 1时尝试重新加载页面。

这是userInfo的我的cookie值:

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

我想要实现的是,一旦允许geoLocation,如果pageCount = 1,则重新加载页面一次。目前,无论pageCount =。

,页面都在重新加载

这是我目前的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;
}

不寻找JQuery解决方案。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

以下代码对我有用......希望这有助于其他人。

// 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;
}
相关问题