Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính navigator.cookieEnabled trong HTML

Thuộc tính navigator.cookieEnabled trong HTML trả về một giá trị boolean cho biết cookie có được bật trong trình duyệt hay không.

Cú pháp

Cú pháp cơ bản:

navigator.cookieEnabled

Ví dụ

Ví dụ sau kiểm tra xem cookie có được kích hoạt trong trình duyệt hay không:

<!DOCTYPE html>
<html>
<style>
  body {
    color: #000;
    height: 100vh;
    background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
    text-align: center;
  }
  .btn {
    background: #db133a;
    border: none;
    height: 2rem;
    border-radius: 20px;
    width: 330px;
    display: block;
    color: #fff;
    outline: none;
    cursor: pointer;
    margin: 1rem auto;
  }
  .show {
    font-size: 1.5rem;
    color: #fff;
  }
</style>
<body>
<h1>Demo thuộc tính navigator.cookieEnabled</h1>
<button class="btn" onclick="display()">Kiểm tra Cookie trình duyệt</button>
<div class="show"></div>
<script>
  function display() {
    document.querySelector(".show").innerHTML = 
      "<p>Cookie trình duyệt có được bật không?</p>" +
      "<p>" + navigator.cookieEnabled + "</p>";
  }
</script>
</body>
</html>

Kết quả

Nhấn nút "Kiểm tra Cookie trình duyệt" để xem cookie có được bật hay không. Giá trị trả về true nếu cookie được bật, false nếu bị tắt.