• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 960
  • Last Modified:

javascript cookie

I am trying to check the browser and write to a cookie what the browser is.  How come my cookie is not writing?


<html>
<head>
<title>Browser detection</Title>
<Script Language="JavaScript">
// Browserdetectionscript made by Henrik Petersen / NetKontoret
// Script explained at www.echoecho.com/javascript.htm
// Please do not remove this and the two lines above.
// Detect the browsername
browsername=navigator.appName;
if (browsername.indexOf("Netscape")!=-1) {browsername="NS"}
else
{if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"}
else {browsername="N/A"}};

//detect the browserversion
browserversion="0";
if (navigator.appVersion.indexOf("2.")!=-1) {browserversion="2"};
if (navigator.appVersion.indexOf("3.")!=-1) {browserversion="3"};
if (navigator.appVersion.indexOf("4.")!=-1) {browserversion="4"};
if (navigator.appVersion.indexOf("5.")!=-1) {browserversion="5"};
if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"};

if (browsername=="NS") {

alert('MOZ');
document.cookie = "browsertype=MOZ";
}

if (browsername=="MSIE"){
  if (browserversion<4)
{
alert('IE');
document.cookie = "browsertype=IE";

}
  if (browserversion>4)
{
alert('IE');
document.cookie = "browsertype=IE";

}
}
</script>
</head>

<body>
</body>
</html>
0
Codeit1978
Asked:
Codeit1978
  • 10
  • 5
  • 5
  • +2
1 Solution
 
ThogekCommented:
How can you tell that it is not writing?
0
 
ThogekCommented:
Note that, if you do not set an explicit expiration for the cookie, then it will be written as a session cookie (valid only until the browser is closed), and session cookies are held in memory, and not written to disk (such as to the Temporary Internet Files folder).

More about the document.cookie property and its usage at http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cookie.asp
0
 
ziffgoneCommented:
On the contrary, Codeit1978, your cookie is being set. Try this and it will show you:

<html>
<head>
<title>Browser detection</Title>
<Script Language="JavaScript">
var set = document.cookie;
if((set.indexOf('NS') > 0) || (set.indexOf('IE') > 0)){
   alert(document.cookie);
   }
   else{
// Browserdetectionscript made by Henrik Petersen / NetKontoret
// Script explained at www.echoecho.com/javascript.htm
// Please do not remove this and the two lines above.
// Detect the browsername
browsername=navigator.appName;
if (browsername.indexOf("Netscape")!=-1) {browsername="NS"}
else
{if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"}
else {browsername="N/A"}};

//detect the browserversion
browserversion="0";
if (navigator.appVersion.indexOf("2.")!=-1) {browserversion="2"};
if (navigator.appVersion.indexOf("3.")!=-1) {browserversion="3"};
if (navigator.appVersion.indexOf("4.")!=-1) {browserversion="4"};
if (navigator.appVersion.indexOf("5.")!=-1) {browserversion="5"};
if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"};

if (browsername=="NS") {

alert('Setting cookie as: MOZ');
document.cookie = "browsertype=MOZ";
}

if (browsername=="MSIE"){
  if (browserversion<4)
{
alert('Setting Cookie as: IE');
document.cookie = "browsertype=IE";

}
  if (browserversion>4)
{
alert('Setting Cookie as: IE');
document.cookie = "browsertype=IE";

}
}
}
</script>
</head>

<body>
</body>
</html>

Simply run it, then hit the "refresh" button on your browser.

Regards...
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ThogekCommented:
Another possible problem....

> browserversion="0";
> if (navigator.appVersion.indexOf("2.")!=-1) {browserversion="2"};

Here, you are setting browserversion as a string, whose single character happens to be a numeric character.  This is not the same as setting it to a number.

> if (browserversion<4)

Here, you are trying to access browserversion as though it were a number, which may not work correctly, and may have unpredicable results.  Try removing the quotes from that first set of lines assigning values to browserversion, so that it's more like:

> browserversion=0;
> if (navigator.appVersion.indexOf("2.")!=-1) {browserversion=2};
0
 
Codeit1978Author Commented:
I can tell that it is not writing by looking at my cookies
0
 
Codeit1978Author Commented:
ziffgone I tired your code but the cookie still did not get written
0
 
Codeit1978Author Commented:
I should see the cookies on my computer correcT?
0
 
Codeit1978Author Commented:
I think you may be right about the expiring, but should it put the cookies in my cookies dir while the page is open?

ziffgone, how do I retrieve that cookie from a different page.  I can see that it is being set, but it does not show up in my cookies folder
0
 
ziffgoneCommented:
"I can see that it is being set, but it does not show up in my cookies folder" - I think this is because an expiry is not being set. Because the cookie is remaining a session cookie, it is not being save to your cookie folder by merely staying in memory until the browser is closed. I imagine that if you set the cookie expiry date, it'll save to your cookie file.

Regards...
0
 
Codeit1978Author Commented:
HOw do i set a simple expiry date?
0
 
ThogekCommented:
What am I, invisible today?

Codeit1978,
Read my comment above (08/15/2004 11:15AM PDT).  Much the same as what ziffgone said a few minutes ago....
0
 
ThogekCommented:
Codeit1978,
See a full description of how to set the various properties ofa cookie (including expiry) at http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cookie.asp
0
 
Codeit1978Author Commented:
I tired to set an expiry date, and now i get java script errors

<html>
<head>
<title>Browser detection</Title>
<Script Language="JavaScript">
var set = document.cookie;
if((set.indexOf('NS') > 0) || (set.indexOf('IE') > 0)){
   alert(document.cookie);
   }
   else{
// Browserdetectionscript made by Henrik Petersen / NetKontoret
// Script explained at www.echoecho.com/javascript.htm
// Please do not remove this and the two lines above.
// Detect the browsername
browsername=navigator.appName;
if (browsername.indexOf("Netscape")!=-1) {browsername="NS"}
else
{if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"}
else {browsername="N/A"}};

//detect the browserversion
browserversion="0";
if (navigator.appVersion.indexOf("2.")!=-1) {browserversion="2"};
if (navigator.appVersion.indexOf("3.")!=-1) {browserversion="3"};
if (navigator.appVersion.indexOf("4.")!=-1) {browserversion="4"};
if (navigator.appVersion.indexOf("5.")!=-1) {browserversion="5"};
if (navigator.appVersion.indexOf("6.")!=-1) {browserversion="6"};

if (browsername=="NS") {

alert('Setting cookie as: MOZ');
document.cookie = "browsertype=MOZ";}

if (browsername=="MSIE"){
  if (browserversion<4)
{
alert('Setting Cookie as: IE');
document.cookie = "browsertype=IE";expires=Mon, 15 Aug 2004 23:59:59 GMT;";
}
  if (browserversion>4)
{
alert('Setting Cookie as: IE');
document.cookie = "browsertype=IE";expires=Mon, 15 Aug 2004 23:59:59 GMT;";
}
}
}
</script>
</head>

<body>

</body>
</html>
0
 
ziffgoneCommented:
My apologies Thogek, it's seems I didn't read your comment thoroughly either, otherwise I would've simply linked to it. :P

Regards...
0
 
ziffgoneCommented:
document.cookie = "browsertype=IE";expires=Mon, 15 Aug 2004 23:59:59 GMT;";

Should be:

document.cookie = "browsertype=IE;expires=Mon, 15 Aug 2004 23:59:59 GMT;";
0
 
Codeit1978Author Commented:
I set the cookie but still cannot view it in my cookies folder.

Can you?
0
 
Codeit1978Author Commented:
I noticed that the Alert('Setting cookies as')
is not showing up.
0
 
ZvonkoSystems architectCommented:
Codeit1978, you have typos:
This:
Alert('Setting cookies as')

Should be:
alert('Setting cookies as')

And this:
document.cookie = "browsertype=IE";expires=Mon, 15 Aug 2004 23:59:59 GMT;";

Should be:
document.cookie = "browsertype=IE;expires=Mon, 16 Aug 2004 23:59:59 GMT;";

0
 
Codeit1978Author Commented:
Ok I figured it out.  Now one last question.  Does mozilla store cookies in a diff location then Internet Explorer?

The reason I want to know this is because in my script once it knows what browser is end user is using it pulls the correct style sheet.  

I keep on running into the issue where it is pulling the IE style sheet in Mozilla
0
 
ziffgoneCommented:
It shouldn't matter. When Mozilla checks for the cookie, it will look the same place where the cookie was set, same for IE, if the mozilla cookie is set in the Mozilla temp folder, which you should be able to find the location of through your browser options/preferences, then when the browser checks for the cookie, it'll look in this temp folder for it. When the script is run in IE, the cookie will be placed in your other temp folder.

Found an error in the code, (and I think it was my error. Whupsies!):

var set = document.cookie;
if((set.indexOf('NS') > 0) || (set.indexOf('IE') > 0)){
   alert(document.cookie);
   }

Should be:
var set = document.cookie;
if((set.indexOf('MOZ') > 0) || (set.indexOf('IE') > 0)){
   alert(document.cookie);
   }
-----------------------------------------------------------

Or, alternatively:

if (browsername=="NS") {
alert('Setting cookie as: MOZ');
document.cookie = "browsertype=MOZ;expires=Mon, 15 Aug 2004 23:59:59 GMT;";
}

could be:
if (browsername=="NS") {

alert('Setting cookie as: NS');
document.cookie = "browsertype=NS;expires=Mon, 15 Aug 2004 23:59:59 GMT;";
}


With my script, we were setting the Netscape cookie as "browsertype=MOZ", but in my "if" statement I gave you, I accidentily set it to look for "NS" instead of "MOZ". Make sure the two of these match and it should work.

Sorry. :P

Regards...
0
 
Michel PlungjanIT ExpertCommented:
Cookie handling when done correctly is transparent to the script
Only issue I have found is that IE will return a 0 length cookie if it is over 4k and mozilla just truncates

Michel
0
 
Codeit1978Author Commented:
That is strange, in php when I try to grab the cookie from IE, it brings back the cookie with the value of IE.  In Mozilla, when I grab the cookie it still says the value is IE.  I even checked the cookie value in Mozillia and it says "MOZ".


The php code i use is $BrowserType=$HTTP_COOKIE_VARS[browsertype];

Should I be using javascript to read the cookie or does it not matter?
0
 
Michel PlungjanIT ExpertCommented:
Sound like a caching problem of PHP
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 10
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now