Solved

javascript cookie

Posted on 2004-08-15
23
944 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
Comment Utility
How can you tell that it is not writing?
0
 
LVL 15

Expert Comment

by:Thogek
Comment Utility
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
Comment Utility
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
 
LVL 15

Expert Comment

by:Thogek
Comment Utility
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
Comment Utility
I can tell that it is not writing by looking at my cookies
0
 
LVL 1

Author Comment

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

Author Comment

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

Author Comment

by:Codeit1978
Comment Utility
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
Comment Utility
"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
Comment Utility
HOw do i set a simple expiry date?
0
 
LVL 15

Expert Comment

by:Thogek
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:Thogek
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I set the cookie but still cannot view it in my cookies folder.

Can you?
0
 
LVL 1

Author Comment

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

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sound like a caching problem of PHP
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now