Add work with cookie

This commit is contained in:
Ilia Miheev
2025-02-01 13:37:15 +03:00
parent c8ceb774ae
commit e114cd1415
+22
View File
@@ -5,6 +5,26 @@ const qrCodeDiv = document.getElementById('qr-code');
const delBtn = document.getElementById('del-qr-btn'); const delBtn = document.getElementById('del-qr-btn');
const textError = document.getElementById('error'); const textError = document.getElementById('error');
// Получить cookie
function getCookie(name) {
let cookies = document.cookie.split("; ");
for (let cookie of cookies) {
let [key, value] = cookie.split("=");
if (key === name) return decodeURIComponent(value);
}
return null;
}
window.onload = () => {
let inputText = getCookie('text');
if (inputText) {
textInput.value = inputText;
} else {
document.cookie = 'text='
}
}
// Вывести сообщение пользователю
function logMessage(message, color = 'red') { function logMessage(message, color = 'red') {
textError.innerText = message; textError.innerText = message;
textError.style.color = color; textError.style.color = color;
@@ -13,6 +33,7 @@ function logMessage(message, color = 'red') {
// Генерация QR-кода // Генерация QR-кода
function generateQRCode() { function generateQRCode() {
const text = textInput.value.trim(); const text = textInput.value.trim();
document.cookie = `text=${text}`
if (!text) { if (!text) {
logMessage('Введите текст для генерации QR-кода!'); logMessage('Введите текст для генерации QR-кода!');
return; return;
@@ -57,4 +78,5 @@ delBtn.addEventListener('click', () => {
textInput.value = ''; textInput.value = '';
qrCodeDiv.innerHTML = ''; qrCodeDiv.innerHTML = '';
textError.innerText = ''; textError.innerText = '';
document.cookie = 'text='
}); });