473,699 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FAQ Topic - How can I see in javascript if a web browser accepts cookies?

-----------------------------------------------------------------------
FAQ Topic - How can I see in javascript if a web browser accepts cookies?
-----------------------------------------------------------------------

Writing a cookie, reading it back and checking if it's the same.

http://www.w3schools.com/js/js_cookies.asp

Additional Notes:

http://www.jibbering.com/faq/faq_notes/cookies.html
===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javas cript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

Dec 10 '06 #1
24 2456
FAQ server wrote:
-----------------------------------------------------------------------
FAQ Topic - How can I see in javascript if a web browser accepts cookies?
-----------------------------------------------------------------------

Writing a cookie, reading it back and checking if it's the same.
Can't you just check window.navigato r.cookieEnabled ?

Andrew Poulos
Dec 11 '06 #2
Andrew Poulos wrote on 11 dec 2006 in comp.lang.javas cript:
FAQ server wrote:
>----------------------------------------------------------------------
- FAQ Topic - How can I see in javascript if a web browser accepts
cookies?
----------------------------------------------------------------------
-

Writing a cookie, reading it back and checking if it's the same.

Can't you just check window.navigato r.cookieEnabled ?
Are there any browsers not implementing this? [ie and ff seem ok]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 11 '06 #3
Evertjan. wrote:
Andrew Poulos wrote on 11 dec 2006 in comp.lang.javas cript:
>FAQ server wrote:
>>----------------------------------------------------------------------
- FAQ Topic - How can I see in javascript if a web browser accepts
cookies?
----------------------------------------------------------------------
-

Writing a cookie, reading it back and checking if it's the same.

Can't you just check window.navigato r.cookieEnabled ?

Are there any browsers not implementing this? [ie and ff seem ok]
It would not matter is there were not as that test doesn't take into
account the actions of content inserting/re-writing proxies which may
re-write the code that references cookies so that it does not (i.e.
change all occurrences of the character sequence 'cookie' into
something else).

Richard.

Dec 11 '06 #4
Richard Cornford wrote on 11 dec 2006 in comp.lang.javas cript:
Evertjan. wrote:
>Andrew Poulos wrote on 11 dec 2006 in comp.lang.javas cript:
>>>Writing a cookie, reading it back and checking if it's the same.

Can't you just check window.navigato r.cookieEnabled ?

Are there any browsers not implementing this? [ie and ff seem ok]

It would not matter is there were not as that test doesn't take into
account the actions of content inserting/re-writing proxies which may
re-write the code that references cookies so that it does not (i.e.
change all occurrences of the character sequence 'cookie' into
something else).
eh?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 11 '06 #5
Evertjan. wrote:
Richard Cornford wrote:
>Evertjan. wrote:
>>Andrew Poulos wrote:
<snip>
>>>Can't you just check window.navigato r.cookieEnabled ?

Are there any browsers not implementing this? [ie and ff seem ok]

It would not matter is there were not as that test doesn't take into
^^
Should be 'if'
>account the actions of content inserting/re-writing proxies which may
re-write the code that references cookies so that it does not (i.e.
change all occurrences of the character sequence 'cookie' into
something else).

eh?
See:-

<URL:
http://groups.google.com/group/comp....d1c4750161a84a
>
Richard.

Dec 11 '06 #6
Richard Cornford wrote on 11 dec 2006 in comp.lang.javas cript:
Evertjan. wrote:
>Richard Cornford wrote:
>>Evertjan. wrote:
Andrew Poulos wrote:
<snip>
>>>>Can't you just check window.navigato r.cookieEnabled ?

Are there any browsers not implementing this? [ie and ff seem ok]

It would not matter is there were not as that test doesn't take into
^^
Should be 'if'
>>account the actions of content inserting/re-writing proxies which
may re-write the code that references cookies so that it does not
(i.e. change all occurrences of the character sequence 'cookie' into
something else).

eh?

See:-

<URL:
http://groups.google.com/group/comp....e_frm/thread/e
128caf75d348100/0bd1c4750161a84 a
>>
I read there:
ZoneAlarm is capable of inserting text into, and re-writing, incoming
HTML pages. It might be that it is preventing client side cookies by
re-writing any occurrences of the identifier 'cookie' that it find
within script sections or pages. That would leave server created HTTP
cookies unaffected (Ebay) and eliminate all client-side JavaScript
cookies.
Well yes, but that does not mean we should programme for any clientside
irregularity.

Cookie writing/reading sequence will not test for cookie cleaning after
browser shutdown, or show the memory-cookie-expire ability either.

=============== =====

I propose window.navigato r.cookieEnabled is worth of inserting in the
FAQ.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 11 '06 #7
Evertjan. wrote:
I read there:
>ZoneAlarm is capable of inserting text into, and re-writing,
incoming HTML pages. It might be that it is preventing client side
cookies by re-writing any occurrences of the identifier 'cookie'
that it find within script sections or pages. That would leave
server created HTTP cookies unaffected (Ebay) and eliminate all
client-side JavaScript cookies.

Well yes, but that does not mean we should programme for any
clientside irregularity.
But surely one should make the effort for a known and not uncommon
possibility, especially when it takes little effort.
Cookie writing/reading sequence will not test for cookie cleaning
after browser shutdown, or show the memory-cookie-expire ability
either.
No method can prove the former - without server-side assistance and
expecting the user to close all browser instances. The latter can be
easily proved: create a cookie (session or future-dated, it doesn't
matter) then reassign that cookie with a date in the past. If the cookie
can be read after the first assignment but not after the second, then it
is reasonable to conclude that the user agent will not send expired
cookies. This approach is easily included while testing the property.
I propose window.navigato r.cookieEnabled is worth of inserting in the
FAQ.
Opposed. It is no more reliable (and possibly less so).

Mike
Dec 11 '06 #8
Michael Winter wrote on 11 dec 2006 in comp.lang.javas cript:
Evertjan. wrote:
>I read there:
>>ZoneAlarm is capable of inserting text into, and re-writing,
incoming HTML pages. It might be that it is preventing client side
cookies by re-writing any occurrences of the identifier 'cookie'
that it find within script sections or pages. That would leave
server created HTTP cookies unaffected (Ebay) and eliminate all
client-side JavaScript cookies.

Well yes, but that does not mean we should programme for any
clientside irregularity.

But surely one should make the effort for a known and not uncommon
possibility, especially when it takes little effort.
>Cookie writing/reading sequence will not test for cookie cleaning
after browser shutdown, or show the memory-cookie-expire ability
either.

No method can prove the former - without server-side assistance and
expecting the user to close all browser instances. The latter can be
easily proved: create a cookie (session or future-dated, it doesn't
matter) then reassign that cookie with a date in the past. If the
cookie can be read after the first assignment but not after the
second, then it is reasonable to conclude that the user agent will not
send expired cookies. This approach is easily included while testing
the property.
We were not discussing what can be eaily done, but what to put in the
FAQ.

Do we put your solution in the FAQ, and then how?
>I propose window.navigato r.cookieEnabled is worth of inserting in
the FAQ.

Opposed. It is no more reliable
That is no reason not to put it in the FAQ.

It is an easy solution, much shorter than a whole function:

if (window.navigat or.cookieEnable d) {...}
(and possibly less so).
Could be, but if so, we should determine that first.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 11 '06 #9
Evertjan. wrote:
Michael Winter wrote on 11 dec 2006 in comp.lang.javas cript:
[snip]
>... create a cookie (session or future-dated, it doesn't matter)
then reassign that cookie with a date in the past. If the cookie
can be read after the first assignment but not after the second,
then it is reasonable to conclude that the user agent will not send
expired cookies. This approach is easily included while testing the
property.
[snip]
Do we put your solution in the FAQ, and then how?
I don't know. It looks a little messy as a single function, but can look
rather intimidating as a collection.

Below is collection of cookie-related methods that I wrote a while ago.
The makeAge method isn't used by the isSupported method.

var Session = function () {
var supported = false;

return {
get: function (name) {
var match = new RegExp('(^|;)\\ s*' + name
+ '\\s*=\\s*([^\\s;]+)').exec(docum ent.cookie);

return match ? match[2] : null;
},
set: function (name, value, maxAge, path, domain) {
var cookie = name + '=' + value;

if (maxAge !== 0) {
var expires = new Date();

expires.setUTCS econds(maxAge
+ expires.getUTCS econds());
cookie += ';expires=' + expires.toGMTSt ring();
}
if (path) cookie += ';path=' + path;
if (domain) cookie += ';domain=' + domain;
document.cookie = cookie;
},
remove: function (name, path, domain) {
this.set(name, 'deleted', -1, path, domain);
},
isSupported: function () {
if (typeof document.cookie == 'string') {
var name = 'test',
value = 'foobar';

while (this.get(name) != null) name += '-' + name;
this.set(name, value, 30);
if ((this.get(name ) == value)
&& (this.get('expi res') == null)) {
this.remove(nam e);
supported = (this.get(name) == null);
}
}
this.isSupporte d = function () {return supported;};
return supported;
},
makeAge: function (days, hours, minutes, seconds) {
return ((days * 24 + (+hours || 0)) * 60
+ (+minutes || 0)) * 60 + (+seconds || 0);
}
};
}();

The age value in the test is arbitrary, really. A value of 2 should
suffice, but a larger number makes no difference. An indication of
success requires the data to be deleted, anyway.
>>I propose window.navigato r.cookieEnabled is worth of inserting in
the FAQ.

Opposed. It is no more reliable

That is no reason not to put it in the FAQ.
It's certainly one reason.
It is an easy solution, much shorter than a whole function:
Is the property guaranteed? Does the property represent client-side
state management in general, or specifically that the document.cookie
property is usable? The former doesn't need to imply the latter; the
former is potentially useful information on its own. Given the nature of
the property, those are not easy questions to answer accurately.

[snip]
>(and possibly less so).

Could be, but if so, we should determine that first.
Richard already presented one reason why it definitely is, though I was
leaning more towards the existence of the property itself. Proving that
to any degree of certainty is impractical, as you should well know.

Mike
Dec 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
333
by: Lucifer | last post by:
Hi I have some code for checking for cookies, that sets a cookie on page1 and checks for it on page2. and its based on the code by MS: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchaspnetcookies101.asp under section: 'Checking Whether a Browser Accepts Cookies' This code works great, when its in my development enviroment.
5
1987
by: Connie Walsh | last post by:
Hi: I gleaned a javascript off of the web: http://www.hypergurl.com/popup.html that sets a cookie everytime someone visits your site. If it is the first visit in x number of days then a popup will appear. It would be great for my application but I can't get it to work. Here is my code:
9
3056
by: SHarris | last post by:
Hello, In our new intranet ASP.NET project, two requirements are that the browser accept cookies AND JavaScript. We are requiring the use of Internet Explorer 6+. 1. Using C# in an ASP.NET application, how can we code the project to check to make sure the browser accepts cookies? 2. Where would we put this code? In Global.asax? 3. Using C# in an ASP.NET application, how can we code the project to detect and make sure the browser...
1
280
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How can I see in javascript if a web browser accepts cookies? ----------------------------------------------------------------------- Writing a cookie, reading it back and checking if it's the same. http://www.w3schools.com/js/js_cookies.asp Additional Notes:
0
8621
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9184
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9042
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8929
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7759
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.