-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (49 loc) · 1.79 KB
/
Copy pathscript.js
File metadata and controls
55 lines (49 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Redirect to dashboard if already logged in
if (window.location.pathname.endsWith("index.html") || window.location.pathname === "/") {
if (localStorage.getItem("username")) {
window.location.href = "dashboard.html";
}
}
function login() {
const username = document.getElementById("username").value;
if (username.trim() !== "") {
localStorage.setItem("username", username);
window.location.href = "dashboard.html";
} else {
alert("Please enter a username.");
}
}
function logout() {
localStorage.removeItem("username");
window.location.href = "index.html";
}
function postComment() {
const comment = document.getElementById("commentInput").value;
if (comment.trim() !== "") {
let comments = JSON.parse(localStorage.getItem("comments")) || [];
comments.push({ user: localStorage.getItem("username"), text: comment });
localStorage.setItem("comments", JSON.stringify(comments));
document.getElementById("commentInput").value = "";
loadComments();
}
}
function loadComments() {
const comments = JSON.parse(localStorage.getItem("comments")) || [];
const section = document.getElementById("commentsSection");
section.innerHTML = "";
comments.forEach(c => {
const div = document.createElement("div");
div.innerHTML = `<strong>${c.user}:</strong> ${c.text}`;
section.appendChild(div);
});
}
// Show username + load comments on dashboard
if (window.location.pathname.endsWith("dashboard.html")) {
const user = localStorage.getItem("username");
if (!user) {
window.location.href = "index.html";
} else {
document.getElementById("userDisplay").innerText = user;
loadComments();
}
}