Hướng dẫn how do you check if cookies are enabled javascript? - làm thế nào để bạn kiểm tra xem cookie có được kích hoạt javascript không?

Thuộc tính Navigator.cookieenables có thể được sử dụng để kiểm tra xem cookie hiện có được bật hay không.navigator.cookieEnabled property can be used to check whether cookies are currently enabled or not.

Cookie có thể được sử dụng cho mục đích theo dõi, vì vậy một số người dùng có thể thích vô hiệu hóa chúng trong trình duyệt của họ. Tuy nhiên, nếu bất kỳ ứng dụng web nào phụ thuộc vào cookie, thì nó sẽ không hoạt động như mong đợi.

Vì vậy, trước khi cố gắng đọc hoặc viết cookie với JavaScript, tốt hơn là kiểm tra xem cookie có được bật hay không. Nếu cookie bị vô hiệu hóa, thì người dùng có thể được thông báo để kích hoạt cookie để tiếp tục hoặc ứng dụng có thể tuân theo logic khác - như không sử dụng bất kỳ cookie nào.

Thay vì tự động giả định rằng cookie phải được bật theo mặc định, và sau đó dẫn đến một sự cố sau đó, tốt hơn là thực hiện kiểm tra trước.

Thuộc tính Navigator.Cookieenables có thể được sử dụng để kiểm tra xem cookie có được bật hay tắt hay không. Điều này trả về đúng nếu cookie được bật hoặc sai nếu cookie bị tắt.navigator.cookieEnabled property can be used to check whether cookies are enabled or disabled. This returns true if cookies are enabled, or false if cookies are disabled.

if(navigator.cookieEnabled) {
	// cookies are enabled
	// read, write and delete cookies
}
else {
	// cookies are disabled, show an error message to the user, or follow other alternative
}

Ví dụ trực tiếp

Cookie có được bật không?

Làm thế nào để phát hiện rằng JavaScript hoặc cookie bị vô hiệu hóa trong trình duyệt của người dùng và thông báo cho anh ta bất kỳ trợ giúp nào?

hỏi ngày 5 tháng 1 năm 2011 lúc 10:59Jan 5, 2011 at 10:59

Hướng dẫn how do you check if cookies are enabled javascript? - làm thế nào để bạn kiểm tra xem cookie có được kích hoạt javascript không?

Mahmoud Salehmahmoud SalehMahmoud Saleh

32.9K118 Huy hiệu vàng333 Huy hiệu bạc491 Huy hiệu Đồng118 gold badges333 silver badges491 bronze badges

2

Để kiểm tra cookie bạn có thể sử dụng:

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();

Và để kiểm tra JavaScript, hãy sử dụng thẻ

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
7 với một số loại tin nhắn bên trong

Hướng dẫn how do you check if cookies are enabled javascript? - làm thế nào để bạn kiểm tra xem cookie có được kích hoạt javascript không?

Đã trả lời ngày 5 tháng 1 năm 2011 lúc 11:01Jan 5, 2011 at 11:01

Robjmillsrobjmillsrobjmills

Phim huy hiệu vàng 18.2K1515 gold badges74 silver badges120 bronze badges

7

Cập nhật (ngày 25/6/18):

Rất nhiều bài viết này, bao gồm cả của tôi, đang lấy đoạn trích từ Modernizr. Tất cả cuối cùng họ sẽ trở nên lỗi thời khi mã hiện đại được cập nhật.

Tôi nghĩ rằng câu trả lời tốt nhất cho câu hỏi này là sử dụng trực tiếp hiện đại.

if (Modernizr.cookies) {
  // supported
} else {
  // not-supported
}

Câu trả lời gốc (5/11/17):

Điều này được lấy trực tiếp từ Modernizr và hoạt động trong nhiều trình duyệt hơn các giải pháp khác trong bài đăng này.

https://github.com/Modernizr/Modernizr/commit/33f00fbbeb12e92bf24711ea386e722cce6f60cc

function checkCookie(){
    // Quick test if browser has cookieEnabled host property
    if (navigator.cookieEnabled) return true;
    // Create cookie
    document.cookie = "cookietest=1";
    var ret = document.cookie.indexOf("cookietest=") != -1;
    // Delete cookie
    document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
    return ret;
}

Đã trả lời ngày 11 tháng 5 năm 2017 lúc 18:57May 11, 2017 at 18:57

Nô -ê Solomonnah SolomonNoah Solomon

1.22314 Huy hiệu bạc23 Huy hiệu đồng14 silver badges23 bronze badges

Vì phát hiện cookie không hoạt động trong IE 11, tôi đề xuất cách tiếp cận hiện đại:

function areCookiesEnabled() {
    try {
      document.cookie = 'cookietest=1';
      var cookiesEnabled = document.cookie.indexOf('cookietest=') !== -1;
      document.cookie = 'cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT';
      return cookiesEnabled;
    } catch (e) {
      return false;
    }
}

https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cookies.js

Hướng dẫn how do you check if cookies are enabled javascript? - làm thế nào để bạn kiểm tra xem cookie có được kích hoạt javascript không?

McMimik

1.10912 Huy hiệu bạc29 Huy hiệu đồng12 silver badges29 bronze badges

Đã trả lời ngày 30 tháng 1 năm 2018 lúc 12:11Jan 30, 2018 at 12:11

ZymotikzymotikZymotik

5.4843 Huy hiệu vàng36 Huy hiệu bạc46 Huy hiệu đồng3 gold badges36 silver badges46 bronze badges

3

Giả sử JavaScript được bật, điều này sẽ cho bạn biết nếu cookie có được bật hay không. Làm việc trong các trình duyệt cũ.

// returns 1 or 0 instead of true or false. Returns null if inconclusive.
function cookiesEnabled() {
    var i, j, cookies, found;
    if (navigator.cookieEnabled===false) return 0;
    document.cookie = 'testcookiesenabled=1';
    for (i=0; i<2; i++) {
        found = false;
        cookies = document.cookie.split(';');
        j = cookies.length;
        while(j--) {
            while (cookies[j].charAt(0)==' ') {// trim spaces
                cookies[j] = cookies[j].substring(1);
            }
            if (cookies[j].indexOf('testcookiesenabled=')==0) {
                found = true;
                break;
            }
        }
        if (!found) {
            return i;
        }
        // Delete test cookie.
        document.cookie = 'testcookiesenabled=; expires=Thu, 01 Jan 1970 00:00:01 GMT';
    }
    // Results inconclusive.
}

Chỉ để hiển thị thông báo khi JavaScript bị vô hiệu hóa, sử dụng


Đã trả lời ngày 8 tháng 4 năm 2019 lúc 23:49Apr 8, 2019 at 23:49

Giáo sư PHP GuruphpPHP Guru

1.19710 Huy hiệu bạc17 Huy hiệu đồng10 silver badges17 bronze badges

// Example[1]: if ( hasCookies() )
/**
* @hasCookies:  Check if cookie's are Enabled
* @param  {none} ''
* @return {BOOLEAN} true|false
*/
function hasCookies() {
return (navigator.cookieEnabled);
}

 // Example[2]: if ( JLS.TEST.COOKIE() )
// Java Pattern ( How to write usable JS)
/** @namespace JLS classes and functions. */
var JLS = JLS || {};
/**
* TEST utility
* @namespace JLS
* @class TEST
*/
JLS.TEST = {

/**
* @public-method COOKIE
* @COOKIE  Check if cookie's are Enabled
* @return {BOOLEAN} true|false
*/
 COOKIE: function () {
    //Change this (library). Not main.js (Algorithm)
    return (navigator.cookieEnabled);
    }
};

Đã trả lời ngày 1 tháng 11 năm 2015 lúc 18:30Nov 1, 2015 at 18:30

Hướng dẫn how do you check if cookies are enabled javascript? - làm thế nào để bạn kiểm tra xem cookie có được kích hoạt javascript không?

3

Đây là cách dễ dàng nhất

if (navigator.cookieEnabled) {
  document.write("Cookies Enabled");
} else {
  document.write("Oops Cookies Not Enabled");
}
Check if Cookies Enabled in Your Browser:  

Hướng dẫn how do you check if cookies are enabled javascript? - làm thế nào để bạn kiểm tra xem cookie có được kích hoạt javascript không?

Galabra

4.9634 Huy hiệu vàng21 Huy hiệu bạc39 Huy hiệu Đồng4 gold badges21 silver badges39 bronze badges

Đã trả lời ngày 1 tháng 5 năm 2017 lúc 3:28May 1, 2017 at 3:28

NireshnireshNiresh

1051 Huy hiệu vàng1 Huy hiệu bạc13 Huy hiệu đồng1 gold badge1 silver badge13 bronze badges

1

Hàm này có thể xem thông báo lỗi cho người dùng và cũng có thể dừng thực thi tập lệnh và nó có thể trả về trạng thái cookie.

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
0

Để dùng nó:

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
1

Và để kiểm tra sử dụng JavaScript:

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
2

Đã trả lời ngày 2 tháng 2 năm 2019 lúc 11:02Feb 2, 2019 at 11:02

Hướng dẫn how do you check if cookies are enabled javascript? - làm thế nào để bạn kiểm tra xem cookie có được kích hoạt javascript không?

Chức năng JavaScript này luôn hoạt động cho tôi:

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
3

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
4
function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
5

Đã trả lời ngày 25 tháng 1 năm 2020 lúc 21:54Jan 25, 2020 at 21:54

Hãy thử thẻ

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
8 Nó hoạt động một phần để kiểm tra xem JavaScript có được bật hay không.

function checkCookie(){
    var cookieEnabled = navigator.cookieEnabled;
    if (!cookieEnabled){ 
        document.cookie = "testcookie";
        cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
    }
    return cookieEnabled || showCookieFail();
}

function showCookieFail(){
  // do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();
6

Dan Jones

1.22711 huy hiệu bạc22 Huy hiệu đồng11 silver badges22 bronze badges

Đã trả lời ngày 28 tháng 12 năm 2012 lúc 15:24Dec 28, 2012 at 15:24

Kamalkamalkamal

Phù hiệu đồng 14966 bronze badges

1

Kích hoạt JavaScript trong Google Chrome..
Mở Chrome trên máy tính của bạn ..
Nhấp chuột. Cài đặt ..
Nhấp vào Bảo mật và Quyền riêng tư ..
Nhấp vào Cài đặt trang ..
Nhấp vào JavaScript ..
Chọn Trang web có thể sử dụng JavaScript ..
Mở chrome.Chuyển đến thêm menu> Cài đặt> Cài đặt trang web> Cookie.Bạn sẽ tìm thấy biểu tượng menu nhiều hơn ở góc trên bên phải.Đảm bảo cookie được bật.Make sure cookies are turned on.
Tạo một cookie với JavaScript JavaScript có thể tạo, đọc và xóa cookie bằng thuộc tính tài liệu.cookie.Với JavaScript, một cookie có thể được tạo như thế này: document.cookie = "username = john doe";Bạn cũng có thể thêm một ngày hết hạn (trong thời gian UTC).with the document.cookie property. With JavaScript, a cookie can be created like this: document.cookie = "username=John Doe"; You can also add an expiry date (in UTC time).
Safari cho iOS (iPhone và iPad) Bước 1: Chuyển đến Cài đặt, sau đó cuộn xuống và chọn Safari.Bước 2: Cuộn xuống quyền riêng tư & bảo mật.Bước 3: Xác minh khối khối tất cả các cookie được đánh dấu (màu xanh lá cây/trắng), bấm vào để cho phép cookie.Bước 4: Xóa bộ nhớ cache của trình duyệt và mở lại trình duyệt.