/
Coderdev
/
web-static-labs
Обзор
Документация
Войти
/
Coderdev
/
web-static-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
html/lab6/task7/script.js
1 строка
2 KB
Coderdev
1
30 мар 2026, 18:31
30 мар 2026, 18:31
020a8dd
Код
Авторство
О чём код?
"use strict";const STORAGE_KEY="lab6SavedProfiles",loadBtn=document.getElementById("loadBtn"),countEl=document.getElementById("count"),statusEl=document.getElementById("status"),profileCard=document.getElementById("profileCard"),avatar=document.getElementById("avatar"),fullName=document.getElementById("fullName"),email=document.getElementById("email"),locationEl=document.getElementById("location");loadBtn.addEventListener("click",loadProfile);window.addEventListener("DOMContentLoaded",updateCount);window.addEventListener("storage",updateCount);function getProfiles(){const raw=localStorage.getItem(STORAGE_KEY);return raw?JSON.parse(raw):[]}function saveProfiles(items){localStorage.setItem(STORAGE_KEY,JSON.stringify(items))}function updateCount(){countEl.textContent=String(getProfiles().length)}async function loadProfile(){statusEl.innerHTML='Загрузка профиля...';try{const response=await fetch("https://randomuser.me/api/");if(!response.ok)throw new Error("HTTP "+response.status);const data=await response.json(),user=data.results[0],profile={id:Date.now(),fullName:`${user.name.last} ${user.name.first}`,email:user.email,location:`${user.location.country}, ${user.location.city}`,avatar:user.picture.large};const profiles=getProfiles();profiles.unshift(profile);saveProfiles(profiles);avatar.src=profile.avatar;fullName.textContent=profile.fullName;email.textContent=profile.email;locationEl.textContent=profile.location;profileCard.hidden=false;updateCount();statusEl.innerHTML='Сохранённых профилей: <strong id="count">'+profiles.length+'</strong>';document.getElementById("count").textContent=String(profiles.length)}catch(error){statusEl.textContent="Не удалось загрузить профиль."}}