| Revision 3915:a57821ba9ef1,
576 bytes
checked in by franck <carnet.franck.paul@…>, 7 years ago
(diff) |
|
Switching from inline JS variables to JSON script. A step ahead…
|
| Line | |
|---|
| 1 | /* exported getData */ |
|---|
| 2 | 'use strict'; |
|---|
| 3 | |
|---|
| 4 | var getData = getData || function(id, clear = true) { |
|---|
| 5 | let data = {}; |
|---|
| 6 | // Read the JSON-formatted data from the DOM. (from https://mathiasbynens.be/notes/json-dom-csp) |
|---|
| 7 | // To be use with: <script type="application/json" id="myid-data">{"key":value, …}</script> |
|---|
| 8 | const element = document.getElementById(`${id}-data`); |
|---|
| 9 | if (element) { |
|---|
| 10 | try { |
|---|
| 11 | data = JSON.parse(element.textContent); |
|---|
| 12 | if (clear) { |
|---|
| 13 | // Clear the element’s contents |
|---|
| 14 | element.innerHTML = ''; |
|---|
| 15 | } |
|---|
| 16 | } catch (e) {} |
|---|
| 17 | } |
|---|
| 18 | return data; |
|---|
| 19 | }; |
|---|
Note: See
TracBrowser
for help on using the repository browser.