1

I made a fairly basic "biomaker" website where the user can edit the card through several <div contenteditable="true">s, and it also uses a simple javascript to allow the user to cycle through several preset colors.

After receiving feedback from several users it seems that it would be better if the page could save the user's previous information upon closing/refreshing the tab.

I did a little research and I came upon the localStorage function but I'm not sure if it can be applied to the color changer and more importantly, the <div contenteditable="true". I'm wondering if 1) it's possible and 2) if so, how I can make it save the content, since what the user puts in the div doesn't affect the backend.

Thanks in advance for any help.

Disclaimer: I've seen a lot of people bashing others because they're asking for "free code". I'm not asking for that here, I'm just hoping people can 1) tell me if it's possible and 2) can point me in the right direction.

EDIT: Thanks for the help! I was able to find a solution.

cm0973
  • 68
  • 1
  • 8
  • You can use it if you want. Don't see much point in it but sure - The changes will only be avalable to the specific user who made the edit – Alon Eitan Feb 11 '21 at 14:58
  • I know that it's only saved on the user's browser, that's the point. If I save it by the id of the div, will it save the changes made by the user? Or is there another way? – cm0973 Feb 11 '21 at 15:07
  • @cm0973 i will provide an answer in 3 minutes – TheEagle Feb 11 '21 at 15:10
  • Your approach is fine IMO, if you don't want to handle it from the server side then go with localstorage – Alon Eitan Feb 11 '21 at 15:11

1 Answers1

0
  1. Yes it is posssible.
  2. You first should check whether localStorage is available in the user's browser, for what you can use this function that works in all browsers (i use it too on my website):
function storageAvailable(type) {
    var storage;
    try {
        storage = window[type];
        var x = "__storage_test__";
        storage.setItem(x, x);
        storage.removeItem(x);
        return true;
    }
    catch (e) {
        return e instanceof DOMException && (e.code === 22 || e.code === 1014 || e.name === "QuotaExceededError" || e.name === "NS_ERROR_DOM_QUOTA_REACHED") && (storage && storage.length !== 0);
    }
}

Use it like this:

editableDiv = dopcument.getElementById("print"); // get the editable div

editableDiv.addEventListener("input", function() { // this function will be executed whenever the content of the editableDiv changes
    if (storageAvailable("localStorage")) { // check for storage availability
        divcontent = editableDiv.innerHTML; // get contents of the editableDiv
        window.localStorage.setItem("divcontent", divcontent); // add them to the localStorage
    } else { // if localStorage is not supported, notify the user about it
        alert("Your browser does not support localStorage or you disabled it, the things you entered could not be saved !");
    }
});

and when the user comes back, you can restore the contents:

editableDiv = document.getElementById("print"); // get the editable div


window.addEventListener("load", function() { // this function is executed whenever the page is loaded
    if (storageAvailable("localStorage")) { // check for storage availability
        divcontent = window.localStorage.getItem("divcontent"); // get the saved divcontent
        if (divcontent === null) { // if the user never modified the divcontent before (or cleaned his localStorage), do nothing
        } else {
            editableDiv.innerHTML = divcontent; // write it into the editableDiv
        }
    } else { // if strorage not available, notify the user
        alert("Your browser does not support localStorage or you disabled it, your previous work could not be restored !");
    }
});

Feel free to modify this code to your needs, and notify me about any problems you have with it ! (couldn't test it, am busy right now)

TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • does it have to be hosted online for it to work? I used that (adjusted it to fit the ids and formatting of a test page) hosted on my pc and I reloaded and the content of an edited div was not saved. (I noticed a couple typos too and fixed those :P but idk if I missed some or something since it's not working) – cm0973 Feb 11 '21 at 15:34
  • @cm0973 it does not have to be hosted online, could you post your js code please ? – TheEagle Feb 11 '21 at 15:40
  • @cm0973 i used the wrong event names; please see if it works now ! – TheEagle Feb 11 '21 at 15:41
  • code's here https://github.com/cornerofrandom/cornerofrandom.github.io/blob/main/test.html and it's still not working for me, not sure why – cm0973 Feb 11 '21 at 15:56
  • I made it so that once edited the "not edited yet" should change to when it was edited. It seems that the `editableDiv.addEventListener("change", function(){....` is never triggered.. any idea why? – cm0973 Feb 11 '21 at 16:11
  • @cm0973 ok, it cannot work since the `print` div is not editable ! You have to do the above for every editable element, let's see if i can code that ... – TheEagle Feb 11 '21 at 16:19
  • I'm not really sure what you mean, as the `print` div is a `contenteditable="true"`. Am I misunderstanding? – cm0973 Feb 11 '21 at 16:20
  • @cm0973 on the hosted webpage i cannot find a `div` with the contenteditable attribute, only `p`'s – TheEagle Feb 11 '21 at 16:24
  • in my code the first `div` is `
    edit me
    `. there aren't any `p`'s in the program. unless are you on the wrong page? the link takes me right to it when i click it
    – cm0973 Feb 11 '21 at 16:27
  • @cm0973 i really looked over the code 3 times and did not find a div with id print, whereas i now found it immediately ! ... – TheEagle Feb 11 '21 at 16:29
  • @cm0973 could you host my code please so that i can test it ? – TheEagle Feb 11 '21 at 16:30
  • your code exactly? I basically copy pasted it, I just added a div to test it on and the date thing to help debug. – cm0973 Feb 11 '21 at 16:42
  • @cm0973 sorry, i cannot find the code in your main.js, seems it takes some time until it is updtaed by the github server – TheEagle Feb 11 '21 at 16:52
  • @cm0973 and i do not know if i will be able to look into this again before tomorrow evening or even after-tomorrow morning – TheEagle Feb 11 '21 at 16:55
  • The script is not in `main.js`, it is uploaded inside `test.html` using ` – cm0973 Feb 11 '21 at 17:01
  • @cm0973 can you send me a link to that test.html file ? – TheEagle Feb 11 '21 at 20:43
  • Sorry for the late reply. It seems the issue with your code is that the `eventListener` is not doing anything. I changed it so that the function triggers when the user clicks a button, and now it works. I'll look into the `change` issue, so that the program can autosave, but for now this is a fairly satisfactory solution. Thank you for your help! – cm0973 Feb 12 '21 at 18:08
  • @cm0973 it seems that there is no `change` event for contenteditable elements, see [this question's answers](https://stackoverflow.com/questions/1391278/contenteditable-change-events) – TheEagle Feb 12 '21 at 20:23
  • sorry for the late reply. thanks for your help, i will look for an alternative solution. – cm0973 Feb 18 '21 at 14:23