/    Sign up×
Community /Pin to ProfileBookmark

I’m Stumped ! Cookie won’t delete

The problem is the function createCookie wont expire (delete) a cookie if passed the variable str, which gets its value from a GridView in ASP.NET. I confirm the str is what I expect with an alert. But if I pass a simple string it works. So my guess is the value of str somehow isnt a true string or something like that?

function delete()
{

var str = document.getElementById(‘GridView1’).rows[1].cells[0].innerText;
alert(‘String is ‘ + str);
createCookie(str,””,-1);

var n1;
n1 = ‘Nutmeg’;
createCookie(n1,””,-1);

return false;
}

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = “; expires=”+date.toGMTString();
}
else var expires = “”;
document.cookie = name+”=”+value+expires+”; path=/”;
//alert(‘cookie = ‘ + document.cookie);
}

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledNov 08.2008 — give us a few example of what can contain the str variable. also, be aware that innerText is msie only property.
Copy linkTweet thisAlerts:
@javajohnauthorNov 08.2008 — Ok str variable could be one word, or two separated by spaces. Examples are "nutmeg" or "basil" or "chocolate chips" ? or "baking soda". When I tested str with "nutmeg" as the innerText it shows ok with an alert, both inside and outside the called function.

Thanks for the tip about innerText being MSIE only. I'll have to plan for that.
Copy linkTweet thisAlerts:
@ZeroKilledNov 08.2008 — i was wondering 'what exactly is wrong?'. however, forgot that [b]delete[/b] is a reserved word for javascript. so, you have to rename you function delete to other thing, perhaps [b]remove[/b]. give a try.
Copy linkTweet thisAlerts:
@javajohnauthorNov 08.2008 — Oh ok. Yes I actually have the function named deleted() but changed it for this post cause I thought it sounded funny! Either way it behaves the same.

To explain what's wrong - the cookie just doesnt get deleted if I pass it the variable str, but the cookie is deleted in the other example in the code where I pass a hardcoded string.

I've tried many ways to cast str as a string, including adding a null string like + '' and using String(str). None helps it seems.

In case this helps, here's some background: The user clicks the asp delete button, which calls the client handler delete() to remove the cookie whose value is in the asp grid.
Copy linkTweet thisAlerts:
@ZeroKilledNov 08.2008 — i'm positive that the function delete work pretty fine. that's, there is no syntax error or wrong logic. tried some test with those function and worked for me. now, the only statement that may bring you unexpected result is the line [b]var str = document.getElementById('GridView1').rows[1].cells[0].innerText;[/b] i know you already told me which are the possible string that returned by previous statement, but i don't which other place to look for.
Copy linkTweet thisAlerts:
@javajohnauthorNov 08.2008 — Yes I agree Zero, that one line is in question. What I don't understand is why the value of str looks ok when I test with alert but it doesnt work the same way as a literal string.?
Copy linkTweet thisAlerts:
@Logic_AliNov 08.2008 — The problem is the function createCookie wont expire (delete) a cookie if passed the variable str, which gets its value from a GridView in ASP.NET. I confirm the str is what I expect with an alert. But if I pass a simple string it works. So my guess is the value of str somehow isnt a true string or something like that?[/quote]I can think of two possible reasons for this:

[LEFT]
[LIST=1]
  • [*]There is a difference between the string being used to create the cookie and the the one used to delete it.

  • [*]The cookie was created in a different folder and the delete function has no access to it, and therefore is creating a different cookie with zero lifetime.

  • [/LIST]


    In the delete function, set the duration to 1 instead of -1 and then alert document.cookie; if two seemingly identical cookies appear, then it must be one of the above.

    [/LEFT]
    Copy linkTweet thisAlerts:
    @justinbarneskinNov 09.2008 — Oh, I was learning a new script and found the [COLOR="Red"]path=/";[/COLOR] to be useful as it allows the cookie to be read across the domain. However, I was frustrated when I couldn't delete the cookie. You may have another cookie with the same name without [COLOR="Red"]path=/";[/COLOR] if so, you'll have to delete that cookie in the specific directory it was created.
    Copy linkTweet thisAlerts:
    @DokNov 09.2008 — Some times cookies are not deleted untill you close the browser session. Did you check that?
    ×

    Success!

    Help @javajohn spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...