469,909 Members | 1,786 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

cookie is sent to server even if it's generated by javascript?

Server sends cookie to browser and the browser send the same cookie
back to the server according to Wikipedia.
Do browsers send even javascript-generated cookie to servers?

For example, if I browse to a page on a server and if the content of
the page is the following:

<html>
<head><script>
alert(document.cookie);
var n = prompt('your new name');
document.cookie = 'name='+n+'; path=/; expires= Fri, 31-Dec-2010
23:59:59 GMT';
</script></head>
<body></body>
</html>

Will the browser send the generated cookie to server next time I visit
the page, even if it's not necessary to send the cookie to server in
this case?

Jan 4 '07 #1
2 6067

HopfZ skrev:
Server sends cookie to browser and the browser send the same cookie
back to the server according to Wikipedia.
Do browsers send even javascript-generated cookie to servers?

For example, if I browse to a page on a server and if the content of
the page is the following:

<html>
<head><script>
alert(document.cookie);
var n = prompt('your new name');
document.cookie = 'name='+n+'; path=/; expires= Fri, 31-Dec-2010
23:59:59 GMT';
</script></head>
<body></body>
</html>

Will the browser send the generated cookie to server next time I visit
the page, even if it's not necessary to send the cookie to server in
this case?
Yes, it will send the cookie to the server next time you visit any page
on that server.

Jan 4 '07 #2
HopfZ wrote:
Server sends cookie to browser and the browser send the same cookie
back to the server according to Wikipedia.
Do browsers send even javascript-generated cookie to servers?
Yes, the cookie is always sent back, since "Cookie: " becomesa line in
the HTTP-header set if the browser finds out that the domain in
question holds cookie-data for it. But for javascript-cookies that
doesn't matter; they reside in the browser's own memory and are not
derived from the HTTP-headers (possibly the start value of
document.cookie might get its content from the HTTP-headers (?) but
certainly not afterwards).
For example, if I browse to a page on a server and if the content of
the page is the following:

<html>
<head><script>
alert(document.cookie);
var n = prompt('your new name');
document.cookie = 'name='+n+'; path=/; expires= Fri, 31-Dec-2010
23:59:59 GMT';
</script></head>
<body></body>
</html>

Will the browser send the generated cookie to server next time I visit
the page, even if it's not necessary to send the cookie to server in
this case?
Yes, always (supposed the browser is allowed to do so, obviously).

Actually the core mechanism of cookies is quite simple, server could
say:

Content-type: text/html
Set-Cookie: foo=bar; path=/; expires Mon, 09-Dec-2002 13:46:00 GMT

and when browser requests a page from the same domain, then it will do
something like

Content-type: text/html
Cookie: foo=bar

Now javascript cookies differ significantly from server-side cookies,
since javascript cookies can be edited dynamically/repeatedly from
within a web page. Traditional cookies (as the example transfer above)
can only belong to one single HTTP request.

But javascript has access to the internal memory of the browser, which
allows the browser to read and write cookiedata on-the-fly. It doesn't
have the limitations of HTTP because javascript is executed on the
client, and cookies reside at the client as well. So the browser can
directly read from and write to local (cookie-)files. Changes to
cookies from javascript take place immediately.

Hope this helps,

--
Bart

Jan 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by CsaaGuy | last post: by
3 posts views Thread by Nathan Sokalski | last post: by
1 post views Thread by GpOscar | last post: by
21 posts views Thread by TonyV | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.