2017-03-28 16 views
0

cookie(userInfo)の値にpageCount = 1が含まれている場合にのみ、ページを再読み込みしようとしています。クッキー文字列からCookie値を取得するjavascript

これは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 

何がPAGECOUNT = 1その後、一度ページをリロードした場合、ジオロケーションが、許可された後ということです。現在、pageCount =に関係なく、ページはリロードされています。

// 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のソリューションを探していない:

は、ここに私の現在のJavascriptです。あなたの助けのための

おかげ

+0

あなたのコードには、明らかにクッキーゲッター機能がありません – hindmost

+0

あなたはどのようにこれを行うかもしれないの例がありますか? – DBoi

+0

あなたは簡単に例を見つけることができます:[1](http://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript)、[2](http://stackoverflow.com/questions/5639346/javascriptで名前でクッキーを読むための最短の機能など)などです。 – hindmost

答えて

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; 
} 
関連する問題