homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Erase subdomain cookies

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

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




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

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)

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


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


if you want to keep non www and kill www,


At least, I'd give it a try.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved