[SalesForce] Delete cookie in Javascript

I would like to delete my cookie in visualforce using Javascript based on the delid.

This is what I've tried so far:

<script> 
 function deletecook() {
  date = new Date();
  date.setDate(date.getDate() -2);
    document.cookie = "id=" + '{!delid}' + "; expires=" + date;
     alert(document.cookie);
     }
</script>

<apex:commandLink action="{!delete}" rerender="thePageBlock" onClick="return deletecook();">
                            <apex:param assignTo="{!delid}" value="{!acc.id}"/>

 </apex:commandLink>

However, the delid of the cookie does not get deleted. Is there any way I could delete the cookie by the id? Thank you.

Best Answer

This question is kind of off-topic, but the correct format would be:

document.cookie = '{!delid}=; expires=1970 Jan 01 00:00:00 GMT';

Specifying id={!delid} specifies that you want to manipulate a cookie named id, setting it to {!delid}.

There's no need to specify an expiry date that's relative to "now", just use the traditional Unix epoch.

Furthermore, actually, deletecook() won't work the way you expect, because onClick occurs before the re-render takes place. You'll probably want to use onComplete.


Edit: Just do this:

<apex:commandLink rerender="thePageBlock"
 onClick="document.cookie='{!acc.id}=; expires=1970 Jan 01 00:00:00 GMT'" />