Advertisement
gandalfbialy

Untitled

May 30th, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. // Ustawia cookie o danej nazwie, wartości i okresie ważności (dni)
  2. function setCookie(name, value, days) {
  3. let expires = "";
  4. if (days) {
  5. const date = new Date();
  6. date.setTime(date.getTime() + days * 24*60*60*1000);
  7. expires = "; expires=" + date.toUTCString();
  8. }
  9. document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/";
  10. }
  11.  
  12. // Odczytuje cookie o podanej nazwie; zwraca wartość lub null
  13. function getCookie(name) {
  14. const nameEQ = name + "=";
  15. const ca = document.cookie.split(";");
  16. for (let c of ca) {
  17. c = c.trim();
  18. if (c.indexOf(nameEQ) === 0) {
  19. return decodeURIComponent(c.substring(nameEQ.length));
  20. }
  21. }
  22. return null;
  23. }
  24.  
  25. // Aktualizuje (lub inicjuje) cookie "licznik" i wyświetla je w <span id="licznik">
  26. function updateCounter() {
  27. let count = getCookie("licznik");
  28. if (count === null) {
  29. count = 1; // pierwsza wizyta
  30. } else {
  31. count = parseInt(count, 10) + 1;
  32. }
  33. setCookie("licznik", count, 7); // cookie ważne 7 dni
  34. document.getElementById("licznik").textContent = count;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement