2016-05-14 2 views
1

私はdocument.cookie属性で練習していました。私はFireFoxブラウザーでCookieを設定して返しました。しかし、私はGoogle Chromeでいくつかのクッキーを返そうとしたが、何も返さなかった。それには理由がありますか?私のコードはjsfiddleです。Google ChromeでCookieを返すことができないのはなぜですか?

HTML

<div id="ex1"> 
    <h2>Example 1</h2> 
    <p></p> 
    <h4>results:</h4> 
    <button id="btn">Return Cookie</button> 
    <button id="btn2">Set Cookie</button> 
</div> 

Javascriptを

function cText(text) { 
    return document.createTextNode(text); 
} 

function cElem(elem) { 
    return document.createElement(elem); 
} 

function attach(id, text, elem) { 
    var a = cText(text); 
    var b = cElem(elem); 
    b.appendChild(a); 

    var c = document.getElementById(id).appendChild(b); 
    return c; 
} 

document.getElementById('btn').onclick = function() { 
    var a = document.cookie; 
    attach('ex1', a, 'p'); 
} 

/* In order to make key = values you have to make a separate line for 
each name and value that you are going to put into the document.cookie*/ 
document.getElementById('btn2').onclick = function() { 
    document.cookie = "name = Michael Hendricks; expires =" + dayt + ";"; 
    document.cookie = "occupation = Shitty Retail Clerk; expires =" + dayt + ";"; 
    document.cookie = "age = 26 years old with big ol man boobs; expires =" + dayt + ";"; 

    console.log('cookie has been saved'); 
} 
+0

あなたのjsfiddleは私の50.0.2661.102でうまくいきます。 –

+0

@Gosha_Fightenはい、それはjfiddleで動作します。しかし、私はそれを通常のhtmlファイルに追加し、それをchromeで出力しても、値は返されません。 –

答えて

0

あなたがローカルホスト上でクッキーを設定した場合、ドメインはNULLでなければなりません。

+0

どのようにドメインをnullにしますか? –

0

Chromeは 'file://'プロトコル(およびサファリ)にCookieを設定しません。 'Web Server for Chrome'を使用して、ローカルWebアプリをテストできます。

関連する問題