2

I dynamically create some a tags with JavaScript when clicking on other elements. When I refresh the page, the created elements disappear. Is there a way to store the creation of the elements in a JavaScript cookie?

function showTab(ev) {
    let head = document.getElementById('heading');
    let node = document.createElement("a"); 
    node.classList.add("tab");
    node.appendChild(document.createTextNode(ev.target.innerHTML));
    head.appendChild(node); 
};
8
  • When I refresh the page, the created elements disappear. Sure. It's designed by all the browsers. Commented Nov 15, 2018 at 7:22
  • I know that they do. But is there a way to prevent it. Commented Nov 15, 2018 at 7:23
  • JS cookies Commented Nov 15, 2018 at 7:23
  • @Jean-MarcZimmer But how to store the created elements there? Commented Nov 15, 2018 at 7:25
  • 1
    Yes, you can save the outerHTML of the tag. When the page is loaded, check the cookie contains some tag or not. If contains, override it. If you are going to use cookie, make the string as short as possible before saving. Commented Nov 15, 2018 at 7:40

3 Answers 3

1

Cookies are probably not the right way to go about something like this as there are size limitations

A better approach would be to store added elements inside the browser's localStorage or indexedDB. You can see an example of the latter in this simple todo app: http://todomvc.com/examples/react/#/

If you inspect the localStorage on that todo example after adding some items, you will see that there is an array containing your entries in JSON format.

Depending on what type of information you need to save, you could either save the HTML directly, or you could save JSON containing the relevant information (tab name, link URL, etc).

I would recommend against saving the HTML directly in case you ever need to make changes to the HTML on your site. Your users would then be loading up outdated HTML.

Sign up to request clarification or add additional context in comments.

2 Comments

@temp - I just made an edit regarding why I believe it would be a bad idea to save HTML directly.
Okay, got it. :)
1

You can store outerHTML of created element in localStorage and get it on page load

var node = document.createElement("a");
node.classList.add("tab");
node.textContent = "new tag";     

var local = localStorage.getItem("html");
if (local == null){
  localStorage.setItem("html", node.outerHTML);
  console.log('HTML setted in localStorage');
} else
    console.log(local);

Because localStorage doesn't worked in snippet you should check it in jsfiddle

Note that if your html content is large you should consider max size of local storage

1 Comment

Thanks, that's what I needed!
0

Okay, I solved it like that:

// Restore

window.onload = function() {
let head = document.getElementById('heading');
archive = [];
key = allStorage();

for(var i=0; i<archive.length; i++){ 
    if(archive[i] != null){
        let node = document.createElement("a");
        node.classList.add("tab");
        node.innerHTML = key[i];
        
        head.appendChild(node);
    }
}
}


//Store all

function allStorage() {
keys = Object.keys(localStorage);           
archive = [];

for (var i=0; i< keys.length; i++) {
    archive.push(keys[i]);
}
return archive;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.