I would like to have a html file with JavaScript, which (file) is able to modify its context. In more details, I imagine it like that. I have a html file, which I open with a browser. I have a text area there where I type my text and press submit button. As a result of that, the context of the form saved somewhere in the html file. What is the easiest and stable way to do that?
-
1If you're talking about permanent storage, you will need a server side language or service for this. Can you use any? – Pekka Oct 04 '10 at 09:40
-
4I would like to do everything on my local computer. I know it's possible without a serve side language. A an working example I can mention TiddlyWiki. – Roman Oct 04 '10 at 09:44
-
Why don't you have a look at it then? – Felix Kling Oct 04 '10 at 09:46
-
TiddlyWiki works using Java in Safari, but achieves this natively in Firefox. I'm not sure how widly supported those features are. Another possibility is the new HTML5 local storage, which works like a database and can store (usually) up to 5MB per site. The downside of course is, that if you open the site in one browser, the content in another browser won't get updated. – Georg Schölly Oct 04 '10 at 09:49
-
1If you download TiddlyWiki, you'll see it actually comes with *two* files — the JAR is what does the saving. – Chuck Oct 04 '10 at 10:13
-
TiddlyWiki is a wiki in a single html file. But I can't tell you how they write/save the data. But you may want to look at their source code: http://www.tiddlywiki.com :-) – Philippe Gerber Oct 04 '10 at 09:49
1 Answers
TiddlyWiki saves all its content to a new, local html-with-javascript file in browser-specific ways. This is because writing to the local hard drive is not normally allowed in javascript, for security reasons. If you're interested in specifically how TiddlyWiki writes the file, check the source code, starting with:
function saveFile(fileUrl,content)
{
var r = mozillaSaveFile(fileUrl,content);
if(!r)
r = ieSaveFile(fileUrl,content);
if(!r)
r = javaSaveFile(fileUrl,content);
return r;
}
This requires the user to explicitly override security warnings. When I tried it in Firefox, I had to do so several times. This is not good practice, as a user would be sorely tempted to check "Remember this decision" and potentially expose themselves to malware in the future.
As someone else said, a better idea is to use client-side storage such as the new features in HTML 5 (available in newer browsers), or a more portable library such as Google Gears; or perhaps better, YUI's StorageUtility, which abstracts to a higher level and uses either HTML 5, Google Gears, or SWF depending on what's available.
-
Note that the discussion about portability of client-side storage options is out of date. – LarsH May 10 '13 at 20:28
-
1TiddlyWiki used a Java applet to implement this feature, but most browsers no longer support applets. I wonder if it's possible to do this using the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API) instead. – Anderson Green Jun 17 '22 at 23:26