我想这样做的一种方法是在另一个域(不是子域,根据定义,但我没有测试过)上有一个脚本,它只是设置一个 cookie,然后脚本可以返回 JSONP,这样你就可以使用它了另一个域上的 javascript。所以像:
PHP ( cookie.php )
<?php
header("Content-type: application/javascript");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
if(isset($_GET["set"])){ //first run here
setcookie("cookie_test","cookies",time()+3600);
//redirect to self after setting cookie so we don't have to call script twice
header('Location: cookies.php?&callback='.@$_GET["callback"]);
die(); //death
}
//once redirected should go here
$cookie_set=array("cookies"=>isset($_COOKIE["cookie_test"]));
echo @$_GET["callback"]. "(" . json_encode($cookie_set) . ")";
Javascript/Jquery
//callback=? for jquery to know it's jsonp. &set so the script sets first
$.getJSON("http://another.domain.com/cookies.php?callback=?&set")
.done(function(data){
if(data.cookies){
//third party cookies are enabled
} else {
//third party cookies probably disabled
}
})
演示。在 Mac 上的 Safari 上进行了测试。