Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="pl" dir="ltr">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JavaScript - ciasteczka</title>
  6. </head>
  7. <body>
  8. <form id="ciacho" action="#" name="theForm">
  9. <input type="text" id="nazwa" placeholder="nazwa"/><br>
  10. <input type="text" id="wartosc" placeholder="wartość"/><br>
  11. <input type="text" id="czas" placeholder="czas w sekundach"/><br>
  12. <button type="button" onclick="utworzCiasteczko()">Utwórz ciasteczko</button>
  13. <br>
  14. </form>
  15. <form id="ciacho" action="#" name="theForm">
  16. <input type="text" id="nazwa2" placeholder="nazwa"/><br>
  17. <button type="button" onclick="sprawdzCiasteczko()">Sprawdź ciasteczko</button>
  18. <br>
  19. </form>
  20. <form id="ciacho" action="#" name="theForm">
  21. <input type="text" id="nazwa3" placeholder="nazwa"/><br>
  22. <button type="button" onclick="usunCiasteczko()">Usuń ciasteczko</button>
  23. <br>
  24. </form>
  25. <script type="text/javascript">
  26. function utworzCiasteczko() {
  27. let nazwa = document.getElementById('nazwa').value;
  28. let wartosc = document.getElementById('wartosc').value;
  29. let czas = document.getElementById('czas').value;
  30.  
  31. let d = new Date();
  32. d.setTime(d.getTime() + (czas * 1000));
  33. let expires = "expires=" + d.toUTCString();
  34.  
  35. document.cookie = nazwa + "=" + wartosc + "; expires=" + expires;
  36. }
  37.  
  38. function sprawdzCiasteczko() {
  39. let name = document.getElementById('nazwa2').value + "=";
  40. let decodedCookie = decodeURIComponent(document.cookie);
  41. let ca = decodedCookie.split(';');
  42. for (let i = 0; i < ca.length; i++) {
  43. let c = ca[i];
  44. while (c.charAt(0) === ' ') {
  45. c = c.substring(1);
  46. }
  47. if (c.indexOf(name) === 0) {
  48. alert(c.substring(name.length, c.length));
  49. }
  50. }
  51. }
  52.  
  53. function usunCiasteczko()
  54. {
  55. let nazwa = document.getElementById('nazwa3').value;
  56.  
  57. document.cookie = nazwa + "=; expires=hu, 01 Jan 1970 00:00:00 UTC;";
  58. }
  59. </script>
  60. </body>
  61. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement