Can you store intermediate values ​​in a session using javascript? What are the disadvantages of this method, if it exists?

For example, I need to store the value on the basis of which the script behavior on the page will be set.

In this example, it is currentAtomicNumber , which compares the value obtained from the attribute, below is a piece of code:

 function openElementDetailBlock(event) { var target = event.target; var currentAtomicNumber; while (target != this) { if (target.hasAttribute('data-number')) { var atomicNumber = target.getAttribute('data-number'); if (atomicNumber != currentAtomicNumber) { elementDetailBlock.classList.toggle('element-detail_opened'); setTimeout(elementDetailBlock.classList.add('element-detail_opened'), 1000); currentAtomicNumber = atomicNumber; } } target = target.parentNode; } } 

One solution I see is the use of a hidden field, an attribute that changes to the resulting value, but this solution does not seem to me to be too correct.

Tell me, what can be the ways of saving the value if JS is not suitable for this task?

  • What session is meant? - Grundy
  • @Grundy user session, before closing the browser. - while1pass
  • What can be the ways of saving the value - saving for what? - Grundy
  • 3
  • @Grundy save any value in order to use the script. gave an example where we read the attribute that should be compared with the stored value. Suppose if you click on a unique element, we show its detailing - while1pass

1 answer 1

To store data for the duration of the session on the client, you can use sessionStorage - this is the same as localStorage - but this is cleared at the time of the end of the session.


An alternative solution to cookies - in the absence of the expires field, the duration of the cookies - is the time of the session.


The disadvantage of both methods is that only strings can be stored, so to work with objects they will have to be serialized / deserialized.

In addition, cookies may be disabled in the browser, and Storage may either not be supported at all, on any devices / browsers or, for example, in the Safari private browser on iOS, you will not be able to write something to it.

  • so far from two options I am satisfied with Storage. the more it is supported by a large number of browsers caniuse.com/#search=localStorage . Thanks for the drawbacks of both methods, you need to read more - while1pass