File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed
Expand file tree Collapse file tree 3 files changed +76
-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+ < link rel ="stylesheet " href ="styles.css " />
7+ < title > Caching Mechanism App</ title >
8+ </ head >
9+ < body >
10+ < div class ="container ">
11+ < h1 > Caching Mechanism App</ h1 >
12+ < div >
13+ < label for ="dataInput "> Enter Data:</ label >
14+ < input
15+ type ="text "
16+ id ="dataInput "
17+ placeholder ="Type something... "
18+ />
19+ < button onclick ="cacheData() "> Cache Data</ button >
20+ </ div >
21+ < div >
22+ < h2 > Cached Data:</ h2 >
23+ < p id ="cachedData "> </ p >
24+ </ div >
25+ </ div >
26+
27+ < script src ="script.js "> </ script >
28+ </ body >
29+ </ html >
Original file line number Diff line number Diff line change 1+ // Function to cache data in localStorage
2+ function cacheData ( ) {
3+ const dataInput = document . getElementById ( "dataInput" ) ;
4+ const cachedData =
5+ localStorage . getItem ( "cachedData" ) || "No data cached yet" ;
6+
7+ // Get the input value
8+ const newData = dataInput . value . trim ( ) ;
9+
10+ // Update cached data in localStorage
11+ localStorage . setItem ( "cachedData" , newData ) ;
12+
13+ // Update the displayed cached data
14+ document . getElementById (
15+ "cachedData"
16+ ) . textContent = `Cached Data: ${ newData } ` ;
17+ }
18+
19+ // Initial load: Display cached data if available
20+ window . onload = function ( ) {
21+ const cachedData =
22+ localStorage . getItem ( "cachedData" ) || "No data cached yet" ;
23+ document . getElementById (
24+ "cachedData"
25+ ) . textContent = `Cached Data: ${ cachedData } ` ;
26+ } ;
Original file line number Diff line number Diff line change 1+ body {
2+ font-family : Arial, sans-serif;
3+ display : flex;
4+ align-items : center;
5+ justify-content : center;
6+ height : 100vh ;
7+ margin : 0 ;
8+ }
9+
10+ .container {
11+ text-align : center;
12+ }
13+
14+ input {
15+ padding : 5px ;
16+ margin-right : 10px ;
17+ }
18+
19+ button {
20+ padding : 5px 10px ;
21+ }
You can’t perform that action at this time.
0 commit comments