govin
govin

Reputation: 6733

Javascript to delete cookie on android web browser

What is the javascript to delete a cookie on android web browser. The usual method of setting an expiration date of the cookie to a date in the past does not work in android web browser.

For e.g. the below code works in desktop web browsers and mobile safari but does not work in android web browser.

document.cookie = 'cookiename=cookievalue; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/';

Upvotes: 15

Views: 3060

Answers (4)

fedeghe
fedeghe

Reputation: 1317

that works for me

document.cookie = yourCookieName + '=' + // NO value here 
    ';expires=Thu, 01-Jan-1970 00:00:01 GMT';

if used path and/or domain at set time, add em

document.cookie = yourCookieName + '=' + // NO value here
    ';path=' + yourCookiePath +
    ';domain=' + yourCookieDomain +
    ';expires=Thu, 01-Jan-1970 00:00:01 GMT';

Upvotes: 1

cosjav
cosjav

Reputation: 2115

Have you tried also including the domain in the cookie setting line? I remember that in some cases you had to be very explicit with the domain and path matching (or being compatible with) the current document location in order to delete a cookie:

document.cookie='cookiename=cookievalue; path=/; domain=current-domain; expires=Thu, 01 Jan 1970 00:00:01 GMT';

Upvotes: 1

fast
fast

Reputation: 885

I've seen browsers (actually in TV sets, but not sure which it was exactly), that did not accept the 'expires=' field (with an absolute date), but worked well with 'max-age=' (live-time in number of seconds from now). So maybe try to delete the cookie by:

document.cookie = 'cookiename=; max-age=0; path=/';

Upvotes: 2

keithmaxx
keithmaxx

Reputation: 649

While this is a roundabout way of doing so, create and instantiate a JavascriptInterface that will be called from the webpage Javascript. Set that Javascript interface to your Webview using

webSettings.setJavascriptEnabled(true);

and

webView.addJavascriptInterface(new JavaScriptInterfaceImplementation(), INTERFACE_NAME);

On your web page you then invoke

javascript:INTERFACE_NAME.yourSessionClearingMethod();

which should contain

CookieManager cookieManager = CookieManager.getInstance();        
cookieManager.removeAllCookie(); // or cookieManager.removeSessionCookie();

to clear said unwanted cookie(s).

Upvotes: -1

Related Questions