Search code examples
javascriptjqueryjquery-jtable

Save current js object in history.pushState


I am trying to save my current js object in history.

history.pushState($(this)[0],'List',window.location.href);

But i am getting error

DataCloneError: The object could not be cloned. history.pushState($(this)[0],'List',window.location.href);


Solution

  • https://developer.mozilla.org/en-US/docs/Web/API/History_API

    The state object can be anything that can be serialized. Because Firefox saves state objects to the user's disk so they can be restored after the user restarts the browser, we impose a size limit of 640k characters on the serialized representation of a state object. If you pass a state object whose serialized representation is larger than this to pushState(), the method will throw an exception. If you need more space than this, you're encouraged to use sessionStorage and/or localStorage.

    So you are serializing with JSON.stringify for data.

     history.pushState(JSON.stringify($(this)[0]),'List',window.location.href);