繁体   English   中英

从Cookie字符串JavaScript获取Cookie值

[英]Getting cookie value from cookie string javascript

我仅在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解决方案。

谢谢你的帮助

以下对我有用的代码...希望对您有所帮助。

// 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM