File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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 > Cookie Demo</ title >
7+ < script >
8+ function setTheme ( theme ) {
9+ document . cookie = "theme=" + theme + "; path=/; SameSite=None; Secure"
10+ applyTheme ( )
11+ }
12+
13+ function getCookie ( name ) {
14+ let match = document . cookie . match ( new RegExp ( '(^| )' + name + '=([^;]+)' ) )
15+ return match ? match [ 2 ] : null
16+ }
17+
18+ function applyTheme ( ) {
19+ let theme = getCookie ( "theme" )
20+ document . body . style . backgroundColor = theme || "white"
21+ }
22+
23+ window . onload = applyTheme
24+ </ script >
25+ </ head >
26+ < body >
27+ < h1 > Cookie-Based Demo</ h1 >
28+ < button id ="blue-btn " onclick ="setTheme('lightblue') "> Light Blue</ button >
29+ < button id ="green-btn " onclick ="setTheme('lightgreen') "> Light Green</ button >
30+ < button id ="reset-btn " onclick ="setTheme('white') "> Reset</ button >
31+ < p > Open this page in multiple tabs/windows to see the shared cookie behavior.</ p >
32+ < p > If you open it in a separate user context, the theme will reset.</ p >
33+ </ body >
34+ </ html >
You can’t perform that action at this time.
0 commit comments