0

I am currently creating an offline HTML page that displays a list of elements with checkboxes. but if I close and reopen the page, my checkboxes are all unchecked (normal). so, I created a "save" button, using the "a" element, that looks like this :

var file = new Blob([data], {type: 'application/text'});
var a = document.createElement("a");
url = URL.createObjectURL(file);
a.href = url;
a.download = 'testSaveJson.js';
document.body.appendChild(a);
a.click();

I save my data as a js file and this file is open in a in my HTML, but I was wondering 2 things :

1 - Is there was a way to do this download in a specific location, without having the browser asking where to download the file, and overwrite the previous file if there is already one with the same name ?

2 - is there a better (and reliable) way to save local information about my web page, for example, by using cookies ? is there a risk that my browser loses my data if I do not reopen my page for a long time with this kind of solution ?

I am still a beginner, and this is really for local use only (this page is not designed to work on line and contains links to local files on my computer)

Thanks for your answers

  • https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Client-side_storage but will always be deleted if browser cleaned. – john Smith Apr 03 '21 at 14:34

1 Answers1

0

1st Question

It is the most important policy that a web application never ever is allowed to load a file from the local file system without user interaction. The same is for storing a file to disk, with the exception that you can usually tell the browser in its settings to store the file without asking for permission. But in Firefox, for example, you cannot chose a combination of "don't ask and overwrite". Either it's "don't ask an store a new copy" or "ask whether to overwrite". It depends fully on the browser.

2nd question

It is highly recommended to store non volatile information into a file. There are ways to store it in the browser's storage, but if you accidentally clear the cache you lose everything.

  • Thanks for this answer. I think I will simply use the "localStorage" solution. What I am trying to save is only some kind of "config parameters", so it is not that bad if I accidentaly clean my cache and have to re-config sometimes. I was trying to avoid doing it every time I re-open the page – Cyril Leconte Apr 03 '21 at 15:37
  • You're welcome. –  Apr 03 '21 at 21:53