2014-09-01 72 views
-1

如果cookie未啓用JavaScript,如何顯示錯誤消息?我知道如何使用PHP,但我想使用JavaScript intead。我試過尋找答案,但沒有成功。如果cookie未啓用,如何顯示錯誤消息?

+0

https://stackoverflow.com/questions/2167310/how-to-show-a-message-only-if-cookies-are-的完整副本禁用瀏覽器| OR | https://stackoverflow.com/questions/703761/check-if-the-client-accepts-cookie-in-javascript – Xatenev 2014-09-01 07:44:05

+0

但仍然沒有答案。 – sfdsfds 2014-09-01 07:47:02

+0

??在所有3個線程中,你都能得到答案您可以輕鬆地谷歌頭,你有1000個問題的解決方案。 – Xatenev 2014-09-01 07:48:10

回答

3

你可以寫這樣的功能,

function checkCookie(){ 
    var cookieEnabled=(navigator.cookieEnabled)? true : false 
    if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
     document.cookie="testcookie"; 
     cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false; 
    } 
    return (cookieEnabled)?true:showCookieFail(); 
} 

function showCookieFail(){ 
    alert('Please enable cookie'); 
} 
+0

我試過這樣做: function checkCookie(){ var cookieEnabled =(navigator.cookieEnabled)? true:false if(typeof navigator.cookieEnabled ==「undefined」&&!cookieEnabled)document.cookie =「testcookie」; cookieEnabled =(document.cookie.indexOf(「testcookie」)!= - 1)?真假; } return(cookieEnabled)?true:showCookieFail(); } 功能showCookieFail(){ 警報( '測試') } 但它並沒有制定出(無警報) – sfdsfds 2014-09-01 07:47:55

+0

@KhairoulIkhwan你叫''checkCookie''功能的onload? – devo 2014-09-01 07:49:17

+0

還沒有,但後來,仍然沒有制定出 – sfdsfds 2014-09-01 07:52:18