s a
s a

Reputation: 1

Document.cookie does not seem to set the cookie

I am trying to set a cookie when a user clicks on a button (as a simple web form). The code I am trying to use looks like this:

<input type="button" onclick="document.cookie = 'q=1';" value="Add to Cart" />

It seems simple enough, but every debugger I'm using says there are no cookies being set, and when I try to access the cookie later it doesn't work. This is giving me innumerable headaches and I'd appreciate help.

Thanks.

Upvotes: 0

Views: 2062

Answers (1)

user1567453
user1567453

Reputation: 2095

Strange because it works for me....

<input id="mybtn" type="button" onclick="document.cookie = 'q=1';" value="Add to Cart" />

So that would indicate to me that somewhere in your internet options you have security set to not accept cookies from certain sources I'd say. Try relaxing your security settings and see how you go (try in a different browser even).

Click here for instructions to enable cookies

Here is a jsfiddle where it's working for me (you will have to use firefox/chrome because IE doesn't like it)

ALSO the issue about not being able to access the cookie later could be for multiple reasons depending on HOW you are trying to access it. Of course if it doesn't exist then you can't access it but I'd say you would start a whole new quest for accessing the cookie (after you read some doco and try some things)

Upvotes: 1

Related Questions