Craig Angus
Craig Angus

Reputation: 23178

how to set a cookie in the address bar?

I want to add a cookie so that I can exclude my interaction with my website from google analytics (I don't have access to put files on server as is third party application)

Is it possible to set a cookie with javascript by executing code in the address bar of the browser?

Upvotes: 6

Views: 21234

Answers (5)

Alex VII
Alex VII

Reputation: 930

I think, what you mean is:

javascript:void(document.cookie="cookiename=value");

Hope this could help you.

Update:

For new browser version you have to also enable javascript to be executed in the address bar.

Upvotes: 3

gcb
gcb

Reputation: 14548

javascript:document.cookie="name=value"

Upvotes: 9

user1438910
user1438910

Reputation:

I don't know if pure JavaScript can do that but i use simple PHP code to do that :

if (isset($request->get['tracking']) && !isset($request->cookie['tracking'])) {
    setcookie('tracking', $request->get['tracking'], time() + 3600 * 24 * 1000, '/');
}

put it in your index.php to save cookie when page loads - if i get your purpose correctly .

*because you want addressee bar , it is better to use PHP and Get request .

Hope it help .

Upvotes: 0

surgbc
surgbc

Reputation: 11

Yes its possible to change cookie values, or to create new cookies from the address bar. I am not so good with javascript, but this should help you change cookie values:

javascript:alert(window.c=function a(n,v,nv) {c=document.cookie;c=c.substring(c.indexOf(n) +n.length,c.length);c= c.substring(1,((c.indexOf(";")>-1) ?  c.indexOf(";") : c.length)); nc=unescape(c).replace(v,nv); document.cookie= n+"="+escape(nc);return unescape(document.cookie);}); alert(c(prompt("cookie name:",""), prompt("replace this value:",""), prompt("with::","")));

And this, to create new cookies

 javascript:document.cookie = cookieName + '=; expires=3600;' +"path=/; domain=" +
                    window.location.hostname;

Upvotes: -3

Diego Cassinera
Diego Cassinera

Reputation: 11

another way is to use firebug on explorer or the js console in ie8. just type document.cookie="XDEBUG_SESSION_START=netbeans-xdebug";

then you can verify it was set by typing document.cookie

Upvotes: 1

Related Questions