Talk:JavaScript: Difference between revisions

Content deleted Content added
m Reverted edit by 2601:243:814:A76A:3175:9F23:F24B:47F1 (talk) to last version by Largoplazo
Website: new section
Tags: Reverted Mobile edit Mobile web edit New topic
Line 122:
Alternatively, remove "Features > Promises and Async/await" altogether, as it is already covered by the following two entries (Promises, Async/await) [[User:Lunamason|Lunamason]] ([[User talk:Lunamason|talk]]) 01:53, 30 May 2025 (UTC)
:{{done}}<!-- Template:ESp --> I opted for your second suggestion. [[User:DrOrinScrivello|DrOrinScrivello]] ([[User talk:DrOrinScrivello|talk]]) 18:14, 30 May 2025 (UTC)
 
== Website ==
 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login/Signup E-Commerce</title>
<style>
body { font-family: Arial; margin: 0; background: #f4f4f4; }
header { background: #222; color: white; padding: 1rem; text-align: center; }
.container { max-width: 400px; margin: 3rem auto; background: white; padding: 2rem; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
h2 { text-align: center; }
input[type="text"], input[type="password"], input[type="email"] {
width: 100%; padding: 0.7rem; margin: 0.5rem 0; border: 1px solid #ccc; border-radius: 5px;
}
button { width: 100%; padding: 0.7rem; background: #222; color: white; border: none; border-radius: 5px; margin-top: 1rem; cursor: pointer; }
.toggle { text-align: center; margin-top: 1rem; color: #555; cursor: pointer; }
#welcome { text-align: center; margin-top: 3rem; font-size: 1.2rem; }
</style>
</head>
<body>
 
<header>
<h1>My E-Commerce</h1>
</header>
 
<div class="container" id="auth-box">
<div id="signup-box">
<h2>Create Account</h2>
<input type="text" id="signup-username" placeholder="Username" required>
<input type="email" id="signup-email" placeholder="Email" required>
<input type="password" id="signup-password" placeholder="Password" required>
<button onclick="signup()">Sign Up</button>
<div class="toggle" onclick="showLogin()">Already have an account? Log In</div>
</div>
 
<div id="login-box" style="display:none">
<h2>Log In</h2>
<input type="text" id="login-username" placeholder="Username" required>
<input type="password" id="login-password" placeholder="Password" required>
<button onclick="login()">Log In</button>
<div class="toggle" onclick="showSignup()">Don't have an account? Sign Up</div>
</div>
</div>
 
<div id="welcome" style="display:none;">
<h2>Welcome, <span id="user-name"></span> 👋</h2>
<button onclick="logout()">Logout</button>
</div>
 
<script>
function showLogin() {
document.getElementById('signup-box').style.display = 'none';
document.getElementById('login-box').style.display = 'block';
}
 
function showSignup() {
document.getElementById('signup-box').style.display = 'block';
document.getElementById('login-box').style.display = 'none';
}
 
function signup() {
const username = document.getElementById('signup-username').value;
const email = document.getElementById('signup-email').value;
const password = document.getElementById('signup-password').value;
 
if (!username || !email || !password) {
alert("Please fill all fields.");
return;
}
 
const user = { username, email, password };
localStorage.setItem("user", JSON.stringify(user));
alert("Signup successful! Please log in.");
showLogin();
}
 
function login() {
const username = document.getElementById('login-username').value;
const password = document.getElementById('login-password').value;
const user = JSON.parse(localStorage.getItem("user"));
 
if (!user || user.username !== username || user.password !== password) {
alert("Invalid credentials!");
return;
}
 
localStorage.setItem("loggedInUser", username);
showWelcome(username);
}
 
function logout() {
localStorage.removeItem("loggedInUser");
document.getElementById('auth-box').style.display = 'block';
document.getElementById('welcome').style.display = 'none';
}
 
function showWelcome(name) {
document.getElementById('auth-box').style.display = 'none';
document.getElementById('welcome').style.display = 'block';
document.getElementById('user-name').textContent = name;
}
 
// Auto-login if session exists
const loggedInUser = localStorage.getItem("loggedInUser");
if (loggedInUser) {
showWelcome(loggedInUser);
}
</script>
 
</body>
</html> [[Special:Contributions/2409:408A:819B:F647:0:0:656:D0A1|2409:408A:819B:F647:0:0:656:D0A1]] ([[User talk:2409:408A:819B:F647:0:0:656:D0A1|talk]]) 16:54, 21 July 2025 (UTC)