|
| 1 | +/** |
| 2 | + * Exercise 1 |
| 3 | + * ============================================ |
| 4 | + * |
| 5 | + * Prompt the user to accept/deny cookies. When they make their choice, |
| 6 | + * display the appropriate section. Store their response locally, so that |
| 7 | + * reloading the page still displays their preference. |
| 8 | + * |
| 9 | + * Implement the "reset" button to allow them to reset their choice. |
| 10 | + */ |
| 11 | + |
| 12 | +let cookiesEnabled = JSON.parse(localStorage.getItem('cookiesEnabled')) |
| 13 | + |
| 14 | +const showUsingCookies = () => { |
| 15 | + document.getElementById('ex1-cookie-consent').style.display = 'none' |
| 16 | + document.getElementById('ex1-using-cookies').style.display = 'block' |
| 17 | +} |
| 18 | + |
| 19 | +const showNotUsingCookies = () => { |
| 20 | + document.getElementById('ex1-cookie-consent').style.display = 'none' |
| 21 | + document.getElementById('ex1-not-using-cookies').style.display = 'block' |
| 22 | +} |
| 23 | + |
| 24 | +const storeChoice = (enabled) => { |
| 25 | + localStorage.setItem('cookiesEnabled', enabled.toString()) |
| 26 | +} |
| 27 | + |
| 28 | +document.getElementById('ex1-cookie-yes').addEventListener('click', () => { |
| 29 | + cookiesEnabled = true |
| 30 | + showUsingCookies() |
| 31 | + storeChoice(true) |
| 32 | +}) |
| 33 | + |
| 34 | +document.getElementById('ex1-cookie-no').addEventListener('click', () => { |
| 35 | + cookiesEnabled = false |
| 36 | + showNotUsingCookies() |
| 37 | + storeChoice(false) |
| 38 | +}) |
| 39 | + |
| 40 | +document.getElementById('ex1-reset').addEventListener('click', () => { |
| 41 | + localStorage.removeItem('cookiesEnabled') |
| 42 | + document.getElementById('ex1-cookie-consent').style.display = 'block' |
| 43 | + document.getElementById('ex1-using-cookies').style.display = 'none' |
| 44 | + document.getElementById('ex1-not-using-cookies').style.display = 'none' |
| 45 | +}) |
| 46 | + |
| 47 | +if (cookiesEnabled != undefined) { |
| 48 | + cookiesEnabled ? showUsingCookies() : showNotUsingCookies() |
| 49 | +} |
| 50 | + |
| 51 | +// (cookiesEnabled && cookiesEnabled !== undefined) ? showUsingCookies() : showNotUsingCookies() |
0 commit comments