Advertisement

Responsive Advertisement

Menyimpan di local storage dengan Javascript atau php

----------------------------------------------
JS
----------------------------------------------
//define a function to set cookies
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}

//get your item from the localStorage
var myItem = localStorage.getItem('myItem');
setCookie('cookieName', myItem, 7);

----------------------------------------------
PHP
----------------------------------------------

$myItem = '';

if(isset($_COOKIE['cookieName'])) {
$myItem = $_COOKIE['cookieName'];
}

if (window) {
// akses local storage
}

atau

window && // akses local storage
// CREATE
localStorage.setItem('hello', 'Hello World!'); // Menyimpan string 'Hello World' dengan key 'hello'

// READ
localStorage.getItem('hello'); // mengambil key 'Hello World!'

// REMOVE
localStorage.removeItem('hello'); // Menghapus key 'hello'

// REMOVE ALL
localStorage.clear(); // Semua local storage pada domain kamu akan dihapus

Posting Komentar

0 Komentar