// Menyimpan datalocalStorage.setItem('username','john_doe');localStorage.setItem('preferences',JSON.stringify({ theme:'dark', fontSize:'16px'}));// Mengambil dataconstusername=localStorage.getItem('username');constpreferences=JSON.parse(localStorage.getItem('preferences'));// Menghapus datalocalStorage.removeItem('username');// Menghapus semua datalocalStorage.clear();// Event listener untuk storage changeswindow.addEventListener('storage', (e) => {console.log('Storage changed:',e.key,e.newValue);});
6.1.2 SessionStorage
// Menyimpan data sesisessionStorage.setItem('cartItems',JSON.stringify([ { id:1, name:'Product 1' }, { id:2, name:'Product 2' }]));// Mengambil data sesiconstcartItems=JSON.parse(sessionStorage.getItem('cartItems'));// Menghapus data sesisessionStorage.removeItem('cartItems');// Menghapus semua data sesisessionStorage.clear();
6.1.3 IndexedDB
// Membuka databaseconstrequest=indexedDB.open('MyDatabase',1);request.onerror= (event) => {console.error('Database error:',event.target.error);};request.onupgradeneeded= (event) => {constdb=event.target.result;// Membuat object storeconststore=db.createObjectStore('customers', { keyPath:'id', autoIncrement:true });// Membuat indexstore.createIndex('name','name', { unique:false });store.createIndex('email','email', { unique:true });};request.onsuccess= (event) => {constdb=event.target.result;// Menambah dataconsttransaction=db.transaction(['customers'],'readwrite');conststore=transaction.objectStore('customers');store.add({ name:'John Doe', email:'john@example.com' });// Mengambil dataconstgetRequest=store.get(1);getRequest.onsuccess= () => {console.log('Customer:',getRequest.result); };};
6.1.4 Cache API
// Menyimpan response ke cachecaches.open('my-cache').then(cache => {cache.add('/styles.css');cache.add('/script.js');// Atau menggunakan addAllcache.addAll(['/','/styles.css','/script.js','/images/logo.png' ]);});// Mengambil data dari cachecaches.match('/styles.css').then(response => {if (response) {return response; }returnfetch('/styles.css'); });// Service Worker dengan Cacheself.addEventListener('fetch', event => {event.respondWith(caches.match(event.request).then(response => response ||fetch(event.request)) );});
6.2 Drag and Drop
6.2.1 Draggable Elements
<!-- Element yang bisa di-drag --><divdraggable="true"id="dragMe"> Drag saya!</div><script>constelement=document.getElementById('dragMe');element.addEventListener('dragstart', (e) => {e.dataTransfer.setData('text/plain',e.target.id);e.target.classList.add('dragging');});element.addEventListener('dragend', (e) => {e.target.classList.remove('dragging');});</script>
6.2.2 Drop Zones
<divid="dropZone"class="drop-zone"> Drop di sini!</div><script>constdropZone=document.getElementById('dropZone');dropZone.addEventListener('dragover', (e) => {e.preventDefault();e.dataTransfer.dropEffect ='move';});dropZone.addEventListener('drop', (e) => {e.preventDefault();constid=e.dataTransfer.getData('text/plain');constdraggableElement=document.getElementById(id);dropZone.appendChild(draggableElement);});</script>