473,729 Members | 2,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cookies in non-server pages?

Hi all,

I'm fairly new to JavaScript, and couldn't find a way to solve the
following problem:

I've written an application in DHTML/JavaScript, for one because I
want to be able to offer access on the web, but primarily because it
was the easiest way for me to combine GUI and layout in a portable
manner.

In case you wonder, it is a character generator for pen-and-paper
roleplaying game (http://www.eprover.org/ROLESIM/chargen2.html -
German language, but code and comments are (mostly) English).

It is still preliminary, but works fine. However, the first time my
users used it, they immediately wanted to safe their characters for
later updates. I know that JavaScript has no direct access to the
local file system (and for good reasons), but we came up with the idea
of storing a character profile in a cookie.

My first test seems to work, but only when the page is actually served
via a web server. The more frequent use case is that the program is
run from a local file. In tat case, it seems that the cookie is not
set at all.

Is there any way around this problem (short of running a cooperating
webserver on each users computer)? Maybe it's just an issue with
setting the proper domain?

Thanks!

Bye,

Stephan

--
-------------------------- It can be done! ---------------------------------
Please email me as sc****@eprover. org (Stephan Schulz)
----------------------------------------------------------------------------
Jan 23 '06 #1
10 1883
Stephan Schulz wrote:
My first test seems to work, but only when the page is actually served
via a web server. The more frequent use case is that the program is
run from a local file. In tat case, it seems that the cookie is not
set at all.

Is there any way around this problem (short of running a cooperating
webserver on each users computer)? Maybe it's just an issue with
setting the proper domain?


Local files have no domain which is the reason why cookies do not work
reliably via the `file:' URI scheme. However, if it is a _program_ that
is _run_ from a local _file_, there is no reason why this program could
not obtain a file handle for accessing the local filesystem.
PointedEars
Jan 23 '06 #2
In article <43************ ****@PointedEar s.de>, Thomas 'PointedEars' Lahn wrote:
Stephan Schulz wrote:
My first test seems to work, but only when the page is actually served
via a web server. The more frequent use case is that the program is
run from a local file. In that case, it seems that the cookie is not
set at all.

Is there any way around this problem (short of running a cooperating
webserver on each users computer)? Maybe it's just an issue with
setting the proper domain?
Local files have no domain which is the reason why cookies do not work
reliably via the `file:' URI scheme.


Hummm...is there any way to make it work unreliably? For the online
version, it's important to make it work as widely as possible. For the
local copy, it's OK if I can make it run in Safari or Firefox (or,
preferably, both).
However, if it is a _program_ that
is _run_ from a local _file_, there is no reason why this program could
not obtain a file handle for accessing the local file system.


It's JavaScript, embedded in HTML. I consider it a program, but it is
not a stand-alone binary. It's executed by the interpreter of the
browser. If there are file-handling functions in JavaScript, they have
escaped me so far (which would not be a surprise...I' started writing
this thing 2 weeks ago or so, and it's my first JavaScript project).

Thanks anyways!

Bye,

Stephan

--
-------------------------- It can be done! ---------------------------------
Please email me as sc****@eprover. org (Stephan Schulz)
----------------------------------------------------------------------------
Jan 23 '06 #3
Stephan Schulz wrote:
[...] Thomas 'PointedEars' Lahn wrote:
Local files have no domain which is the reason why cookies do not work
reliably via the `file:' URI scheme.
Hummm...is there any way to make it work unreliably?


Perhaps you meant to ask "Is there any way to make it work reliably?" since
it already works unreliably :) The answer would be: No, there is not. A
user agent may or may not support cookies for the `file:' URI scheme.
For the online version, it's important to make it work as widely as
possible. For the local copy, it's OK if I can make it run in Safari
or Firefox (or, preferably, both).


Try this:

<body>
<script type="text/javascript">
document.write( 'document.cooki e = ' + document.cookie + "<br>\n");
document.write( 'document.cooki e = ' + navigator.userA gent);
var d = new Date();
document.cookie = "foo=" + d.toGMTString()
+ "; expires="
+ new Date(d.getTime( ) + 24*3600000).toG MTString()
+ "; path=/home/scripts/test"; // modify to fit your needs
</script>
</body>

Surprising to me, it appears to work reliably in

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20060110
Debian/1.5.dfsg-4 Firefox/1.5 Mnenhy/0.7.3.0

and

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20051007
Debian/1.7.12-1

It is even possible to specify the `path' component to allow `file://'
cookies to work between files in different directories. It is required
that the `domain' component is omitted, i.e. `domain=' or
`domain=localho st' does not work. Note that those cookies will be set
with domain `scheme:file', so if you look for the appropriate cookie rule
(which may have been set by having the checkbox in the cookie message box
checked), you have to look for this.

Cookies for `file://' resources do not appear to work in

Mozilla/5.0 (compatible; Konqueror/3.5; Linux 2.6.14.4-20051220.153223 +0100;
X11; i686; de, en_US) KHTML/3.5.0 (like Gecko) (Debian package 4:3.5.0-3)

which is why I assume they will also not work in Safari (both use KHTML as
layout engine and KJS as script engine): the cookies can be set there (or
so it seems) but they cannot be retrieved later.
However, if it is a _program_ that
is _run_ from a local _file_, there is no reason why this program could
not obtain a file handle for accessing the local file system.


It's JavaScript, embedded in HTML. I consider it a program, but it is
not a stand-alone binary. It's executed by the interpreter of the
browser. If there are file-handling functions in JavaScript, they have
escaped me so far (which would not be a surprise...I' started writing
this thing 2 weeks ago or so, and it's my first JavaScript project).


If you do not want to use cookies (with the restrictions described above),
you will have to use at least UA-specific host objects. For Gecko-based
browsers these would be XPCOM file objects available in privileged script;
for IE, it would be the FileSystemObjec t ActiveX/COM object.

<URL:http://xulplanet.com/references/xpcomref/group_Files.htm l>
<URL:http://msdn.microsoft. com/library/en-us/script56/html/af4423b2-4ee8-41d6-a704-49926cd4d2e8.as p>

As for Safari, I am afraid that the KHTML DOM does not appear to provide for
such an object:

<http://developer.kde.o rg/documentation/library/3.4-api/khtml/html/index.html>

But it is entirely possible that I overlooked something.
PointedEars
Jan 25 '06 #4
In article <11************ ****@PointedEar s.de>, Thomas 'PointedEars' Lahn wrote:
Stephan Schulz wrote:
[...] Thomas 'PointedEars' Lahn wrote:
Local files have no domain which is the reason why cookies do not work
reliably via the `file:' URI scheme.
Hummm...is there any way to make it work unreliably?


Perhaps you meant to ask "Is there any way to make it work reliably?" since
it already works unreliably :)


Hmm, for me the naive way reliably failed to work ;-)
The answer would be: No, there is not. A
user agent may or may not support cookies for the `file:' URI scheme.
For the online version, it's important to make it work as widely as
possible. For the local copy, it's OK if I can make it run in Safari
or Firefox (or, preferably, both).
Try this:

<body>
<script type="text/javascript">
document.write( 'document.cooki e = ' + document.cookie + "<br>\n");
document.write( 'document.cooki e = ' + navigator.userA gent);
var d = new Date();
document.cookie = "foo=" + d.toGMTString()
+ "; expires="
+ new Date(d.getTime( ) + 24*3600000).toG MTString()
+ "; path=/home/scripts/test"; // modify to fit your needs
</script>
</body>

Surprising to me, it appears to work reliably in

[...]

That's very useful, thanks a lot!
If you do not want to use cookies (with the restrictions described above),
you will have to use at least UA-specific host objects. For Gecko-based
browsers these would be XPCOM file objects available in privileged script;
for IE, it would be the FileSystemObjec t ActiveX/COM object.

[...]

Ok. I'll stick with the cookie method that I can probaly make to work
portably for FireFox and for the web version.

Bye,

Stephan

--
-------------------------- It can be done! ---------------------------------
Please email me as sc****@eprover. org (Stephan Schulz)
----------------------------------------------------------------------------
Jan 25 '06 #5
Stephan Schulz wrote:
[...] Thomas 'PointedEars' Lahn wrote:
Stephan Schulz wrote:
For the online version, it's important to make it work as widely as
possible. For the local copy, it's OK if I can make it run in Safari
or Firefox (or, preferably, both).
Try this:

<body>
<script type="text/javascript">
document.write( 'document.cooki e = ' + document.cookie + "<br>\n");
document.write( 'document.cooki e = ' + navigator.userA gent); ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^ var d = new Date();
document.cookie = "foo=" + d.toGMTString()
+ "; expires="
+ new Date(d.getTime( ) + 24*3600000).toG MTString()
+ "; path=/home/scripts/test"; // modify to fit your needs
</script>
</body>

Surprising to me, it appears to work reliably in [...]

^^ [...]

That's very useful, thanks a lot!


D'oh. Did I really post the unrevised version? Use this instead:

<body>
<script type="text/javascript">
document.write( 'document.cooki e = ' + document.cookie + "<br>\n"
+ 'navigator.user Agent = ' + navigator.userA gent);
var d = new Date();
document.cookie = "foo=" + d.toGMTString()
+ "; expires="
+ new Date(d.getTime( ) + 24*3600000).toG MTString()
+ "; path=/home/scripts/test"; // modify to fit your needs
</script>
</body>

You're welcome, anyway :)
Regards,
PointedEars
Jan 25 '06 #6
JRS: In article <11************ ****@PointedEar s.de>, dated Wed, 25 Jan
2006 01:26:57 remote, seen in news:comp.lang. javascript, Thomas
'PointedEars' Lahn <Po*********@we b.de> posted :
document.cookie = "foo=" + d.toGMTString()
+ "; expires="
+ new Date(d.getTime( ) + 24*3600000).toG MTString()
+ "; path=/home/scripts/test"; // modify to fit your needs


If users of that announce a 1-day lifetime, they should be prepared for
complaints at the end of October. Using d.setDate(d.get Date()+1))
would not be significantly slower.

Why do you choose to recommend use of a property which has been
deprecated since 1998 or earlier (or has it been de-deprecated in
acknowledgement of the fact that in javascript (by specification) and in
most computers the time behaves more like GMT than UTC)? You should
read ECMA-262, backwards.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 26 '06 #7
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
document.cookie = "foo=" + d.toGMTString() ^ + "; expires="
+ new Date(d.getTime( ) + 24*3600000).toG MTString()
+ "; path=/home/scripts/test"; // modify to fit your needs
If users of that announce a 1-day lifetime, they should be prepared for
complaints at the end of October.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^ Using d.setDate(d.get Date()+1)) would not be significantly slower.
Nice to know. Why do you assume it was my intention to let the cookie
expire 1 day later?
Why do you choose to recommend use of a property which has been
deprecated since 1998 or earlier (or has it been de-deprecated in
acknowledgement of the fact that in javascript (by specification) and in
most computers the time behaves more like GMT than UTC)?
Because this is but an example for a cookie that will not expire by the end
of the session?
You should read ECMA-262, backwards.


Everybody should; starting with you, as you seem to have too much free time
on your hands, making wild assumptions about the most unimportant details
just to comment anything.
PointedEars
Jan 26 '06 #8
Thomas 'PointedEars' Lahn said the following on 1/26/2006 2:54 PM:
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
document.cookie = "foo=" + d.toGMTString() ^ + "; expires="
+ new Date(d.getTime( ) + 24*3600000).toG MTString()
+ "; path=/home/scripts/test"; // modify to fit your needs If users of that announce a 1-day lifetime, they should be prepared for
complaints at the end of October.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^


As well as in the Spring Time.
Using d.setDate(d.get Date()+1)) would not be significantly slower.


Nice to know. Why do you assume it was my intention to let the cookie
expire 1 day later?


Simple Math, and common sense, my dear Watson. 24*3600000 = 86400000
which divided by 1000 gives 86,400 seconds which is the number most
people incorrectly use to add 1 day to a Date Object. And since you
appear to be attempting to add 1 day to the date then the common sense
answer is that you are attempting to expire the cookie "1 day later".
Why do you choose to recommend use of a property which has been
deprecated since 1998 or earlier (or has it been de-deprecated in
acknowledgement of the fact that in javascript (by specification) and in
most computers the time behaves more like GMT than UTC)?


Because this is but an example for a cookie that will not expire by the end
of the session?


One should endeavor to provide the best code possible, not just "an
example" that is fraught with problems.

And, it *will* expire before the end of the session if the session lasts
more than 24 hours. Note that I did not say exceeds more than 1 day.
You should read ECMA-262, backwards.


Everybody should;


Yes, even if for nothing more than some boring reading.
starting with you, as you seem to have too much free time
on your hands, making wild assumptions about the most unimportant details
just to comment anything.
Coming from you, that is plain hilarious.

PointedEars


Notice the improper signature that you asked, just yesterday, to not be
quoted? Please either properly delimit it or stop claiming it as a
signature.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 27 '06 #9
JRS: In article <11************ *****@PointedEa rs.de>, dated Thu, 26 Jan
2006 20:54:07 remote, seen in news:comp.lang. javascript, Thomas
'PointedEars' Lahn <Po*********@we b.de> posted :
Dr John Stockton wrote:
[...] Thomas 'PointedEars' Lahn [...] posted :
document.cookie = "foo=" + d.toGMTString() ^ + "; expires="
+ new Date(d.getTime( ) + 24*3600000).toG MTString()
+ "; path=/home/scripts/test"; // modify to fit your needs


If users of that announce a 1-day lifetime, they should be prepared for
complaints at the end of October.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
Using d.setDate(d.get Date()+1)) would not be significantly slower.


Nice to know. Why do you assume it was my intention to let the cookie
expire 1 day later?


I assumed nothing about your intention; I was referring to what the
unwitting consumer of your flawed advice might think your code eas good
for.

Why do you choose to recommend use of a property which has been
deprecated since 1998 or earlier (or has it been de-deprecated in
acknowledgement of the fact that in javascript (by specification) and in
most computers the time behaves more like GMT than UTC)?


Because this is but an example for a cookie that will not expire by the end
of the session?


So why use a deprecated method when a non-deprecated one will do the
job? It's not setting a good example.

You should read ECMA-262, backwards.


Everybody should; starting with you, as you seem to have too much free time
on your hands, making wild assumptions about the most unimportant details
just to comment anything.


Now you're being puerile - or is it early Alzheimer's, or just your true
nature showing?

--
© John Stockton, Surrey, UK. ??*@merlyn.demo n.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Jan 27 '06 #10

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

Similar topics

1
5977
by: Michal A. Valasek | last post by:
Hello, I have problem deleting cookies and cookies values (using framework version 1.1 on W2003). When I try to remove entire cookie, by calling Response.Cookies.Remove("Test"), nothing happens, the cookie "Test" remains unchanged. When I try to remove one of cookie values, by calling
1
1425
by: hochun | last post by:
I am trying to read non-persistent cookies that is not reading in the program coding is there any software thich can read it (just like Cookie Monster, but it can't read non-persistent cookies ) Thanks
6
1587
by: Pete Davis | last post by:
I've never done anything with cookies. What I'm trying to do is very straight-forward, but for some reason, it just doesn't seem to want to work. I have a helper class with some static methods. Two are for setting and getting the name of the user to/from a cookie: public static string GetSiteUser(HttpRequest request) { HttpCookie userCookie = request.Cookies; if (null == userCookie || null == userCookie.Value) {
5
3944
by: Mr Newbie | last post by:
I am having trouble writing cookies despite adding the cookie to the Response.Cookeies collection. The Request.Browser.Cookies returns TRUE so I know the browser is storing cookies and I can even see like Google cookies in Documents and Settings/MyName/Cookies however there is never a cookie saved by the name I create it with. As well as this, the Request.Cookies.Count is three but in the directory I just referred to there are hundreds,...
2
2243
by: john.lehmann | last post by:
Attacked is a piece of code which first hits the login page successfully and receives back login cookies. But then when I attempt to hit a page which is restricted to logged in users only, I fail. That seems to be because I am not successfully re-attaching the cookies to the header portion of the this request. I have tried 2 methods which should both work I think. The first was to use install_opener to attach the cookie handler back...
3
1267
by: David Thielen | last post by:
Hi; I need to keep a string in Session however this does not work if the user has cookies turned off. How can I work around this? -- thanks - dave david_at_windward_dot_net http://www.windwardreports.com
1
4466
by: Joe | last post by:
In ASP.NET 1.1 I could detected expired form authentication tickets (which closely coincide with my expired session) by checking for the Authentication Cookie when the login screen loads. If the cookie exists, then decrypt the forms auth. ticket and check to see if it is expired. If so display a message to the user letting them know why they are back on the login screen. The code I used was something like this: Dim cookie as HttpCookie...
2
1232
by: David Thielen | last post by:
Hi; 1) What is the easiest way to show in an aspx page if they have scripts enabled? 2) What is the easiest way to show in an aspx page if they have cookies enabled? This is for a test page we will have so if they are having problems, we can have them bring up that page to see if things are configured right.
4
4047
by: Jeff | last post by:
I have a vb.net application (2005) requiring session variables and want to test to make certain that the user's cookies are enabled. I can set a test session variable on one page and attempt to read it on the next with the code below in order to determine if the user has their browser's cookies enabled, but is there a good way to do this on the first/default page without a redirect to another page that will actually do the test? Thanks
1
1753
by: archana | last post by:
Hi all, Can anyone tell me what exactly persistant cookies and non-persistant cookies means. I read some where that in non persistant cookies if i am using session data remains in that cookies untill end of session. But in persistant cookies, data remains in cookies until it expires.
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9281
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
9200
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
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
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...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
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.