Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取具有特定名称的cookie_Javascript_Php_Html - Fatal编程技术网

Javascript 获取具有特定名称的cookie

Javascript 获取具有特定名称的cookie,javascript,php,html,Javascript,Php,Html,我想检查html中是否存在cookie, 这是我的密码: <!DOCTYPE html> <html> <head> </head> <body> <div id="a1"> <p id = "ttttt">a1 test</p> </div> <script> function checkCookie() { $cookie_name = 'testhastin';

我想检查html中是否存在cookie, 这是我的密码:

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<div id="a1">
<p id = "ttttt">a1 test</p>
</div>
<script>
function checkCookie() {
   $cookie_name = 'testhastin';

    if(!isset($_COOKIE[$cookie_name])) {

    document.getElementById("ttttt").innerHTML = "set";
} else {

    document.getElementById("ttttt").innerHTML = "not set";
}
}
window.checkCookie(); 
</script>

</body>
</html>

a1测试

函数checkCookie(){ $cookie_name='testhastin'; 如果(!isset($\u COOKIE[$COOKIE\u name])){ document.getElementById(“ttttt”).innerHTML=“set”; }否则{ document.getElementById(“ttttt”).innerHTML=“未设置”; } } window.checkCookie();
但当我运行这个页面时,“a1测试”不会发生任何变化。(意味着它不会变为“未设置”)

我想检查页面上是否存在cookie,如果存在,则显示特定内容

我也试过这个代码,结果是一样的:

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<div id="a1">
<p id = "ttttt">a1 test</p>
</div>
<script>
function checkCookie() {
    var username = getCookie("testhastin");


    if (username != "") {
    document.getElementById("ttttt").innerHTML = "set";
} else {

    document.getElementById("ttttt").innerHTML = "not set";
}
}
window.checkCookie(); 
</script>

</body>
</html>

a1测试

函数checkCookie(){ var username=getCookie(“testhastin”); 如果(用户名!=“”){ document.getElementById(“ttttt”).innerHTML=“set”; }否则{ document.getElementById(“ttttt”).innerHTML=“未设置”; } } window.checkCookie();
$\u COOKIE
是一个PHP变量
isset()
是一个PHP function。您不能在JavaScript中使用它

至于获取cookie,请使用
document.cookiename

// Setting a cookie
document.testhastin= "Akshay";
// Reading a cookie
var val = document.testhastin;

W3Schools链接:

注意“a1测试”会发生什么。
这是什么意思?如果没有cookie集,则必须将其更改为
未设置
@AnkitAgarwal,而不会更改为未设置。这是页面的链接:您将得到错误
uncaughtreferenceerror:isset未定义
,这是一个PHP呈现的代码,因此使用PHP进行相应的处理code@AnkitAgarwal你能告诉我该怎么做吗?我更新了我的问题,尝试了另一个代码,但没有得到结果。问题中的getCookie函数在哪里?