JSFiddle - React, Tailwind, and code Playground

by Chris

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Timestamp Storage</title>
</head>
<body>
    <button id="addTimestampButton">Add Timestamp</button>
    <button id="showTimestampsButton">Show Timestamps</button>
    <script>
        document.getElementById('addTimestampButton').addEventListener('click', function() {
            const timestamp = new Date().toISOString();
            addTimestamp(timestamp);
            alert(`Timestamp ${timestamp} added!`);
        });

        document.getElementById('showTimestampsButton').addEventListener('click', function() {
            const timestamps = getTimestamps();
            alert(`Timestamps: ${timestamps.join(', ')}`);
        });
    </script>
</body>
</html>

JavaScript

function addTimestamp(timestamp) {
    const url = window.location.href;
    const key = `timestamps_${url}`;
    let timestamps = JSON.parse(localStorage.getItem(key)) || [];
    timestamps.push(timestamp);
    localStorage.setItem(key, JSON.stringify(timestamps));
}

function getTimestamps() {
    const url = window.location.href;
    const key = `timestamps_${url}`;
    return JSON.parse(localStorage.getItem(key)) || [];
}