954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Setting Cookies with Javascript in Chrome

The following code works fine in IE8 and Firefox 3.5. Any idea why it does not work in Chrome ? Tried it both in localhost and on my web server -- no success.

In the header:
[code = php]
<?php
if((isset($_COOKIE["unique_id"]))&&isset($_COOKIE["users_resolution"])){
$screen_res = $_COOKIE["users_resolution"];
$unique_id = $_COOKIE["unique_id"];
}
else //means cookie is not found set it using Javascript
{
?>
[/code]

[code = html]

[/code]
[code = php]
<?php } ?>

// And in the ,body>

<?php
echo "Screen resolution: ".$screen_res."
";
echo "Unique ID: ".$unique_id."
";
?>
[/code]

Any help would be appreciated.

FeralReason
Light Poster
32 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 
The following code works fine in IE8 and Firefox 3.5. Any idea why it does not work in Chrome?

I haven't tested the javascript part of the code in Chrome yet, but the first place I would look would be the Cookies tab at
Tools | Options | Under the Hood | Privacy [Content settings...]

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

It isn't obvious how this

var the_cookie = "users_resolution="+ screen.width +"x"+ screen.height;
var the_cookie = "unique_id="+ uniqid();
var the_cookie = the_cookie + ";expires=" + the_cookie_date;

could be working in any browser. For starters, the second assignment obliterates the first.

Try something like this

var the_cookie = "users_resolution="+ screen.width +"x"+ screen.height;
    the_cookie += ";unique_id="+ uniqid();
    the_cookie += ";expires=" + the_cookie_date;

instead.

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: