javascripthtmldom-eventsweb-storage

Save EventListener in webstorage


Is it possible to save EventListeners to webstorage?

var entity = document.createElement("a");
entity.addEventListener("click", function(e) {
    play(path, this.id, "audio");
    e.preventDefault();
});

function save() {
    var playlist = document.getElementById("playlist");
    localStorage.setItem("playlist", playlist.innerHTML);
    localStorage.setItem("id", id);
}

This saves the element fine, but when the page is reloaded the listener is gone.


Solution

  • I doubt it, since LocalStorage can only store strings, and it seems like a bit of overkill to just save an event listener into the LocalStorage object, when you don't know that all of your users will even have the capability.

    I would consider a pseudo-element storage, similar to what is discussed here.