Welcome to WebmasterWorld Guest from 54.157.222.62

Forum Moderators: open

Message Too Old, No Replies

Erase subdomain cookies

   
12:46 pm on Apr 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Due to EU cookie laws I'm looking for a Javascript to erase cookies set by Google Analytics on one of my sites.

GA sets cookies on mydomain.com but when I set a consent cookie it is set on www.mydomain.com.

I can easily delete the consent cookie by setting its expiry to the past just by reference to its name but I'm having problems that GA is giving the domain mydomain.com.

I've tried many different scripts like this:

function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}


but I can't get it to work.

Does anyone have an idea how I can delete cookies on the root domain from the www. sub domain?

Many thanks

Sid
2:16 pm on Apr 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've cobbled together a solution that is working in Chrome and Firefox, I've not tried it in other browsers yet.

This in the page head:
function delete_cookie ( cookie_name )
{
document.cookie = cookie_name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/; domain=.mydomain.com;";
}


This on page:
<a href="#" onclick= "delete_cookie('__utma');delete_cookie('__utmb');delete_cookie('__utmc');delete_cookie('__utmz';">Clear Cookies</a>


Seems to be working reliably.
4:46 pm on Apr 19, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You do know that this whole problem goes away with a proper 301 to one or the other, correct? (Pick one, with www or without.) If you can access your site via www and without www, as far as search results go, you're basically dividing the strength among the two domains.

Otherwise, can't you just reset it without deleting it? You just need to specify the domain, I think. This

domain=.mydomain.com;

sets it for mydomain.com, www.mydomain.com, sales.mydomain.com, products.mydomain.com . . . if you want to keep www and not domain,

domain=www.mydomain.com;

if you want to keep non www and kill www,

domain=mydomain.com;

At least, I'd give it a try.