Solved

javascript cookie

Posted on 2004-08-15
23
946 Views
Last Modified: 2008-02-01
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
Comment
Question by:Codeit1978
  • 10
  • 5
  • 5
  • +2
23 Comments
 
LVL 15

Expert Comment

by:Thogek
ID: 11804863
How can you tell that it is not writing?
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11804866
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
 
LVL 14

Expert Comment

by:ziffgone
ID: 11804869
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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 15

Expert Comment

by:Thogek
ID: 11804881
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
 
LVL 1

Author Comment

by:Codeit1978
ID: 11804888
I can tell that it is not writing by looking at my cookies
0
 
LVL 1

Author Comment

by:Codeit1978
ID: 11804898
ziffgone I tired your code but the cookie still did not get written
0
 
LVL 1

Author Comment

by:Codeit1978
ID: 11804904
I should see the cookies on my computer correcT?
0
 
LVL 1

Author Comment

by:Codeit1978
ID: 11804935
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
 
LVL 14

Expert Comment

by:ziffgone
ID: 11805031
"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
 
LVL 1

Author Comment

by:Codeit1978
ID: 11805040
HOw do i set a simple expiry date?
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11805047
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
 
LVL 15

Expert Comment

by:Thogek
ID: 11805052
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
 
LVL 1

Author Comment

by:Codeit1978
ID: 11805068
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
 
LVL 14

Expert Comment

by:ziffgone
ID: 11805070
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
 
LVL 14

Accepted Solution

by:
ziffgone earned 25 total points
ID: 11805078
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
 
LVL 1

Author Comment

by:Codeit1978
ID: 11805097
I set the cookie but still cannot view it in my cookies folder.

Can you?
0
 
LVL 1

Author Comment

by:Codeit1978
ID: 11805113
I noticed that the Alert('Setting cookies as')
is not showing up.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11805503
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
 
LVL 1

Author Comment

by:Codeit1978
ID: 11805983
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
 
LVL 14

Expert Comment

by:ziffgone
ID: 11806064
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 11806065
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
 
LVL 1

Author Comment

by:Codeit1978
ID: 11809245
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 11812582
Sound like a caching problem of PHP
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question