检测第三方cookies在运行时被阻塞;没有页面刷新;
JavaScript; localStorage Cookie; Block third-party cookies and site data;创建动态、独立的HTML文档,其中“项目”首先是脱机的;
通过更改浏览器设置来检测第三方cookie,cookie将继续设置,直到页面被刷新;
示例代码:使用试图捕获“访问”失败(阻止);
if (typeof(Storage) !== "undefined") {
try{
localStorage.setItem("document","test");
localStorage.removeItem("document");
} catch(err) {
alert("Cookies had failed to be set; Blocked!!");
}
}示例代码:使用navigator.cookieEnabled;
if (!navigator.cookieEnabled) {
//Sample Code here;
} else { //Sample Code Here; }
这种情况只检测到没有刷新主cookie设置,而不是第三方数据;
发布于 2017-05-12 14:46:48
这里的解决方案是基于https://github.com/mindmup/3rdpartycookiecheck的解决方案
其中涉及三个文件。客户机文件,让我们称它为ThirdPartyCookies.html,以及另一个服务器(第三方服务器)中的另外两个文件。让我们将这两个文件称为ThirdPartyCookies2.html和ThirdPartyCookies3.html。这是一个全JavasScript解决方案。您的第三方服务器可以是静态CDN。
ThirdPartyCookies.html (客户端):这个文件是客户端文件。不需要刷新页面来测试第三方cookie。通过使用您自己的第三方服务器或CDN的位置来修改代码,其中显示了文件在代码中的位置。
<!DOCTYPE html>
<html>
<head>
<style>
.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;
}
</style>
<script>
window.onload = function (){
var receiveMessage = function (evt) {
if (evt.data === 'MM:3PCunsupported') {
alert("Cookies had failed to be set; Blocked!!");
} else if (evt.data === 'MM:3PCsupported') {
// Third party cookies are supported
}
};
window.addEventListener("message", receiveMessage, false);
};
function samplestorage(){
var iframe = document.getElementById('iframeCookies');
iframe.src = iframe.src;
}
</script>
</head>
<body>
<div class="small">
go at Privacy & 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;
<iframe id="iframeCookies" src="LOCATION OF THE FILE/ThirdPartyCookies2.html" style="display:none" />
</body>
</html>第三方Cookies2.html(在第三方服务器中):
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
document.cookie="thirdparty=yes";
document.location="ThirdPartyCookies3.html";
</script>
</body>
</html>第三方Cookies3.html(在第三方服务器中):
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
if (window.parent) {
if (/thirdparty=yes/.test(document.cookie)) {
window.parent.postMessage('MM:3PCsupported', '*');
} else {
window.parent.postMessage('MM:3PCunsupported', '*');
}
}
</script>
</body>
</html>如果您希望看到其他解决方案,请检查此选项,因此请询问Check if third-party cookies are enabled
发布于 2017-05-11 19:12:31
检查localStorage是否为null
if (localStorage === null) {
alert("localStorage is disabled");
}发布于 2021-10-07 16:13:38
检测第三方cookie状态可能有点麻烦。我经历了这个问题,在网上没有发现任何有用的东西,所以我自己写了一个解决方案,这就是
我想出的方法是添加一个外部iFrame,我们现在就构建它,它将检测iFrames是否可以访问cookie,并将cookie的状态通知父应用程序。尽管听起来很奇怪,但实际上我们无法通过iFrame或visa调用父函数,但我们的袖子下有一个超级大国⚡。
完整的文章和一个例子
https://stackoverflow.com/questions/43923761
复制相似问题