Advertisement
lemansky

Untitled

Mar 21st, 2024
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.64 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Theme Preference</title>
  7. </head>
  8. <body>
  9.     <h1>Theme Preference</h1>
  10.     <label for="themeSelect">Select a Theme:</label>
  11.     <select id="themeSelect">
  12.         <option value="light">Light Theme</option>
  13.         <option value="dark">Dark Theme</option>
  14.     </select>
  15.     <br>
  16.     <button>Save Theme Preference</button>
  17.    
  18.     <script>
  19.         function saveThemePreference() {
  20.             let theme = document.getElementById('themeSelect').value;
  21.             document.cookie = `theme=${theme};`;
  22.             alert(`Theme preference set to ${theme}`);
  23.         }
  24.  
  25.         function getThemePreference() {
  26.             let cookies = document.cookie.split(';').map(cookie => cookie.trim());
  27.             let cookie = cookies.find(cookie => cookie.startsWith('theme='));
  28.             if (cookie) {
  29.                 let theme = cookie.split('=')[1];
  30.                 applyTheme(theme);
  31.             }
  32.         }
  33.  
  34.         function applyTheme(theme) {
  35.             document.body.className = theme;
  36.         }
  37.  
  38.         document.querySelector('button').addEventListener('click', (e) => {
  39.             saveThemePreference();
  40.         });
  41.  
  42.         document.addEventListener('DOMContentLoaded', () => {
  43.             getThemePreference();
  44.         });
  45.     </script>
  46.  
  47.     <style>
  48.         body.light {
  49.             background-color: #f5f5f5;
  50.             color: #333;
  51.         }
  52.  
  53.         body.dark {
  54.             background-color: #333;
  55.             color: #fff;
  56.         }
  57.     </style>
  58. </body>
  59. </html>
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement