Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
devwiki:javascript [2021/09/02 09:48] – [other new ES6 codes] ying | devwiki:javascript [2022/07/31 05:53] – [array operation] ying | ||
---|---|---|---|
Line 42: | Line 42: | ||
* button click confetti: | * button click confetti: | ||
* https:// | * https:// | ||
+ | |||
+ | ====== array operation ====== | ||
+ | * array to string <code javascript> | ||
+ | const fruits = [" | ||
+ | let text = fruits.join(';' | ||
+ | </ | ||
+ | * process array <code javascript> | ||
+ | // process like police man in back-door truck, first in last out, last in first out | ||
+ | let truck_queue = [' | ||
+ | deploy_police = main_queue.pop(); | ||
+ | // add new police | ||
+ | cats.push(' | ||
+ | |||
+ | // process like coffee shop, first in first out, last in last out | ||
+ | let coffee_queue = [' | ||
+ | cur_customer = coffee_queue.shift(); | ||
+ | |||
+ | // also can cut queue to like this | ||
+ | coffee_queue.unshift(' | ||
+ | |||
+ | // also add new to coffee queue normall | ||
+ | coffee_queue.push(' | ||
+ | </ | ||
+ | |||
+ | ====== UI interaction operation ====== | ||
+ | |||
+ | * scroll to top for scrollable element <code javascript> | ||
+ | document.getElementById(" | ||
+ | |||
+ | #css part | ||
+ | .coinbox_content{ | ||
+ | float: left; | ||
+ | width: 430px; | ||
+ | height: 100%; | ||
+ | overflow: auto; | ||
+ | } | ||
+ | </ | ||
+ | ====== HTML5 and Javscript ====== | ||
+ | |||
+ | * store data | ||
+ | * localStorage: | ||
+ | * session storage: store locally while the tab is open, remove once closed | ||
+ | * cookie: store locally and server on requests, across different browser, support old html4, expire by defined expire | ||
+ | * example code <code javascript> | ||
+ | localStorage.setItem(' | ||
+ | console.log(localStorage.getItem(' | ||
+ | |||
+ | localStorage.removeItem(' | ||
+ | localStorage.clear() | ||
+ | |||
+ | // cookie is simple, all in 1 line text, that is for all keys and datas | ||
+ | document.cookie = " | ||
+ | |||
+ | utc_date = new Date(" | ||
+ | console.log(utc_date) | ||
+ | document.cookie = " | ||
+ | document.cookie = " | ||
+ | |||
+ | // read cookie | ||
+ | function getCookie(name) { | ||
+ | const value = `; ${document.cookie}`; | ||
+ | const parts = value.split(`; | ||
+ | if (parts.length === 2) return parts.pop().split(';' | ||
+ | } | ||
+ | console.log(document.cookie) | ||
+ | console.log(getCookie(' | ||
+ | |||
+ | //to remove cookie, just set expire to early date | ||
+ | </ | ||
+ | * note on SameSite attribute needed nowadays to secure cookie been sent to 3rd site: | ||
+ | * option: (SameSite=lax) for normal, (SameSite=strict) for same url, (SameSite=None; | ||
+ | * https:// | ||
+ | * https:// | ||
+ |