Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="pl" dir="ltr">
- <head>
- <meta charset="utf-8">
- <title>JavaScript - ciasteczka</title>
- </head>
- <body>
- <form id="ciacho" action="#" name="theForm">
- <input type="text" id="nazwa" placeholder="nazwa"/><br>
- <input type="text" id="wartosc" placeholder="wartość"/><br>
- <input type="text" id="czas" placeholder="czas w sekundach"/><br>
- <button type="button" onclick="utworzCiasteczko()">Utwórz ciasteczko</button>
- <br>
- </form>
- <form id="ciacho" action="#" name="theForm">
- <input type="text" id="nazwa2" placeholder="nazwa"/><br>
- <button type="button" onclick="sprawdzCiasteczko()">Sprawdź ciasteczko</button>
- <br>
- </form>
- <form id="ciacho" action="#" name="theForm">
- <input type="text" id="nazwa3" placeholder="nazwa"/><br>
- <button type="button" onclick="usunCiasteczko()">Usuń ciasteczko</button>
- <br>
- </form>
- <script type="text/javascript">
- function utworzCiasteczko() {
- let nazwa = document.getElementById('nazwa').value;
- let wartosc = document.getElementById('wartosc').value;
- let czas = document.getElementById('czas').value;
- let d = new Date();
- d.setTime(d.getTime() + (czas * 1000));
- let expires = "expires=" + d.toUTCString();
- document.cookie = nazwa + "=" + wartosc + "; expires=" + expires;
- }
- function sprawdzCiasteczko() {
- let name = document.getElementById('nazwa2').value + "=";
- let decodedCookie = decodeURIComponent(document.cookie);
- let ca = decodedCookie.split(';');
- for (let i = 0; i < ca.length; i++) {
- let c = ca[i];
- while (c.charAt(0) === ' ') {
- c = c.substring(1);
- }
- if (c.indexOf(name) === 0) {
- alert(c.substring(name.length, c.length));
- }
- }
- }
- function usunCiasteczko()
- {
- let nazwa = document.getElementById('nazwa3').value;
- document.cookie = nazwa + "=; expires=hu, 01 Jan 1970 00:00:00 UTC;";
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement