I'd like to create an html (with javascript) file where I'd like to add/remove checkboxes at runtime, save them in the html code so that they are available when the file is opened next time. I do not want to use use a server (so no JSP or PHP).
Please tell me whether it is possible just by using javascript? If it is, please point to resources where I can get more information about it.
It is possible using HTML5 local storage or even client database storage. If you don't know whether the browser supports them yet, another possibility is to use persistent cookies.
I'm pretty sure this isn't possible in the way you request solely in javascript in-the browser (javascript cannot write to the source file).
You might have some luck however storing the definition of what checkboxes you've added in localStorage for example, then reload/redraw that same definition on reload of the page.
Related
I coded a local html file that kind of works like windows notepad would, using localStorage with JavaScript to auto save. To make new notes, I have just been saving the open file as another .html file and naming it something new for the note. But I have found this to be really annoying, because when I want to delete the note, I have to actually go into my Files app, find the .html file for the note, and delete it.
I know how to use event listeners and whatever, but is there a line of code I can use to delete the file or even just ask the user for confirmation to delete the file? I basically need a way for a local html file to be able to delete itself using JavaScript. Everything I have managed to find online says that you need Node.js and it has to be on a server, but I wanted to ask anyway, because I wonder if permissions are different when the file is stored locally and not on a server.
No.
You can't use browser JavaScript to manipulate the real local filesystem. (You could access a sandboxed one, but not the real filesystem.)
I have difficulties with saving a var from javascript to a local file, so I want to write it into an existing file. I can do it with creating and downloading a file but I need it without the browser asking if and where to store the file/var. Is there any way of doing it? Need it because I want to automatically save the var so that my main program (runs locally) can get it. I scrolled through many sites but I couldn't find anything that fit my needs so I unfortunately don't have any code.
Thanks Akira
no there is no way of doing that as it is a security risk and the user needs to confirm the location of the file. The only thing you could do is set a default filename so the user would be required to overwrite the existing file.
If the main programm is running you can have the webpage talk to it via http, websockets or webrtc to localhost.
It may be preferable to use a database instead ? Your main program will retrieve the variables in it.
Our webform has a file upload, and just a regular picture box on it. Now we already have the code to save the pictures and retrieve them, but how would you display the picture they selected without first saving it, then refreshing the page and displaying it? Is this a javascript thing?
Thanks in advance.
Most browsers do not allow this. Filed picked will be submitted with the form, but will not be available to javascript. This is to prevent javascript from having access to local files.
Your best solution is to use AJAX to upload the file, then retrieve it from the server and display it. This solution makes it so the initial view takes longer, but if the user then decides it is the file they want, you can avoid sending it to the server again and just move it from a temporary storage area to a permanent one.
This definitely is not possible with JavaScript. It might be possible with a Flash uploader (but I really don't know Flash). JavaScript does not have any sort of direct access to the host's file system.
On second thought - it might be possible to inject an img tag whose src is a file:// URL. I'm not sure about this, though. Checking now.
In my web app I need to give the user the option to save a js variable as a file (when the user clicks download, the app offers him to save a file, preffereably as .js file).
Similarly as google docs offers you to save a file.
Is it possible for javascript to pass it's variable this way?
Check out Downloadify which allows exactly this.
Downloadify is a tiny JavaScript + Flash library that enables the generation and saving of files on the fly, in the browser, without server interaction.
it requires Flash installed in the user's browser to work, though.
I know of no other way of doing this without server interaction.
You might also want to check out OpenSave:
http://www.gieson.com/Library/projects/utilities/opensave/
... which looks much like Downloadify, but seems to have a few extra features (and not as complicated?).
I need a Javascript sample which reads contents from a url and creates a file in the client with same contents when some button is clicked. I also understand that Javascript cannot be allowed to access the local file system (Unless you use ActiveX objects). I do not want to rely on ActiveX objects (since my client is not IE always).
So here is what I did. I used the standard XmlHttpRequest made a request and got my bytes. Now I thought I could kinda stream this contents to the user, first by opening a standard dialog box (the box that pops up when you attempt download something from internet with options like Open/Save/Cancel) and then asking the user to save it somewhere.
I know how to do the read part, can someone show some javascript/html sample on "How to stream open a confirm dialog box to the user and stream some contents?"
PS: Not too sure if this could be done in javascript at all but with Javascript you never know what is possible and what is not :)
Rather than using Javascript to stream the content, which is not possible for obvious security reasons, you need to point the browser at a URL that will return a 'Content-Type' header of 'application/octet-stream'. In most cases this will force the browser to initiate a 'save as' operation and ask the user what to do with it.
I believe it is possible to do this using an iframe in the same page, such that the user will not have to navigate away from the page or open a new tab/window.
See it this way: if you could read/write to users' computers using JavaScript, then no computer would be safe from browsing the web.
Having said so, you cannot read/write client-side files using JavaScript.
But you could if you use Flash / a Java applet; in those cases the embedded objects asks for your permission before doing such actions.
You may want to have a look at TiddlyWiki that claims it can write itself to the disk...
If I understand what your question is, then you want to use JavaScript to write data like a server-side script would (PHP, Python, Java, etc..) but in browser?
If so, then what your asking isn't possible with in browser JavaScript.
However if for some reason you wanted to do this with server-side JavaScript, then yes the "streaming" part is possible.