M
M
mindgrow2018-02-10 19:32:49
JavaScript
mindgrow, 2018-02-10 19:32:49

JS - Storing data in an html page - how much is correct?

Good evening.
In one of the pages in an asp-net project, I plan to store the date for later actions - in a hidden html div of the page.
In order to subsequently receive this value from several event handler functions.
Tell me how correct it is to use this approach and how else you can create "global" variables in javascript

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Interface, 2018-02-10
@Interface

you can generate a view script:

<script>
    window.varibleName = {yourData: 'foo'};
</script>

then the handlers will have access to window.varibleName
Store directly in text in div'e is at least less convenient, and there are no pluses in this.
Another option is to store something in data-attributes.
If data is needed between reboots, use localStorage

A
Alexander Litvinenko, 2018-02-10
@edli007

Not at all correct.
Use redux as a last resort and local storage will do.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question