如何在没有页面刷新的情况下阻止第三方cookie被阻止; [?]

时间:2017-05-11 19:07:57

标签: javascript try-catch local-storage session-cookies issue-tracking

[?]如何检测第三方Cookie 已在运行时被阻止;没有页面刷新;
的JavaScript; localStorage Cookie;阻止第三方Cookie和网站数据;

  

示例代码:使用尝试捕获"访问"失败(被阻止);

if (typeof(Storage) !== "undefined") {
  try{
    localStorage.setItem("document","test");
    localStorage.removeItem("document");
  } catch(err) {
    alert("Cookies had failed to be set; Blocked!!");
  }
}

[❗]现在是如果已经加载的页面,从浏览器设置阻止它将不会触发指示它被浏览器阻止的事件; 除非刷新页面;
- 对于动态生成的文档似乎不太好;通过继续浏览文档而丢失的值;
[^]意味着触发事件继续工作设置cookie直到刷新丢失;所以在此之前不会显示警报;

  

运行Code Snippet Sample ::



function samplestorage() {
  if (typeof(Storage) !== "undefined") {
    try {
      localStorage.setItem("document", "test");
      localStorage.removeItem("document");
    } catch (err) {
      alert("Cookies had failed to be set; Blocked!!");
    }
  }
}

.testbutton {
  background-color: blue;
  color: yellow;
  text-align: center;
  border: 1px solid #000000;
  border-radius: 8px;
  min-width: 100px;
  max-width: 100px;
  cursor: pointer;
}

.testbutton:hover {
  background-color: darkgreen;
  color: yellow;
}

.small {
  font-family: "Verdana";
  font-size: x-small;
}

<div class="small">
  go at Privacy &amp; Security, under browser settings, "Chrome,Opera"<br> then check\uncked [ ] Block third-party cookies and site data;
</div>

<br>

<div class="testbutton" onclick="samplestorage();">
  Test Button
</div>

<br> page has to be refreshed if cookie settings are changed at browser settings;
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

检查localStorage是否为null

if (localStorage === null) {
  alert("localStorage is disabled");
}