js Archives - SCLOUD http://soya.moe Featured Webservices Sun, 05 Aug 2018 05:10:28 +0000 ko-KR hourly 1 https://wordpress.org/?v=6.0.11 http://soya.moe/wp-content/uploads/2018/06/cropped-icon-1-32x32.png js Archives - SCLOUD http://soya.moe 32 32 How copy to the clipboard. http://soya.moe/archives/882 http://soya.moe/archives/882#respond Sat, 04 Aug 2018 04:27:29 +0000 http://soya.moe/?p=882 Original Code (ES5) var copyBtn = document.getElementById('copyBtn'); copyBtn.addEventListener('click', function() { var copyTxt = document.getElementById('copyTxt'); copyTxt.focus(); copyTxt.select(); document.execCommand('copy'); });   Fixed...

The post How copy to the clipboard. appeared first on SCLOUD.

]]>
Original Code (ES5)

var copyBtn = document.getElementById('copyBtn');

copyBtn.addEventListener('click', function() {
var copyTxt = document.getElementById('copyTxt');

copyTxt.focus();
copyTxt.select();

document.execCommand('copy');
});

 

Fixed Code (ES6)

let copyBtn = document.getElementById('copyBtn');

copyBtn.addEventListener('click', () => {
let copyTxt = document.getElementById('copyTxt');

copyTxt.focus();
copyTxt.select();

document.execCommand('copy');
});

 

JSFiddle

 

 

Screenshots

 

Comment & Discussion

 

document.execCommand('copy'); 를 이용해서 text를 클립보드로 복사합니다. 복사할 때 해당 텍스트에 focus()로 위치를 잡고 select()로 드래그해서 선택한 것과 같이 만듭니다.

The post How copy to the clipboard. appeared first on SCLOUD.

]]>
http://soya.moe/archives/882/feed 0
How to store array or JSON obj in localstorage. http://soya.moe/archives/864 http://soya.moe/archives/864#respond Wed, 01 Aug 2018 09:59:42 +0000 http://soya.moe/?p=864 Javascript Code (ES5) // set array to localstorage // param "key" should be string type (ex. "keyName") function setLocalArray(key, data)...

The post How to store array or JSON obj in localstorage. appeared first on SCLOUD.

]]>
Javascript Code (ES5)

// set array to localstorage
// param "key" should be string type (ex. "keyName")
function setLocalArray(key, data) {
localStorage.setItem(key, JSON.stringify(data));
}

// param "key" should be string type (ex. "keyName")
function getLocalArray(key) {
return JSON.parse(localStorage.getItem(key));
}

// testArray
var testArray = [
"hello",
"world",
"javascript"
];

 

Javascript Code (ES6)

// set array to localstorage
// param "key" should be string type (ex. "keyName")
const setLocalArray = (key, data) => {
localStorage.setItem(key, JSON.stringify(data));
}

// param "key" should be string type (ex. "keyName")
const getLocalArray = (key) => {
return JSON.parse(localStorage.getItem(key));
}

// testArray
let testArray = [
"hello",
"world",
"javascript"
];

 

Console

setLocalArray("testArrayKey", testArray);
let recvArray = getLocalArray("testArrayKey");
console.log(recvArray);
console.log(recvArray.length);

 

Screenshots

 

Comment & Discussion

 

JSON 오브젝트를 저장할 때도 동일한 방법으로 JSON.stringify, JSON.parse를 통해 string에서 JSON 오브젝트를 추출하면 됩니다. 위의 코드를 예로 들면 recvArray.property와 같은 방법으로 객체의 각 속성에 접근할 수 있습니다.

localstorage에 저장된 데이터를 삭제하고 싶을 때는 localStorage.removeItem(key); 메서드를 통해서 선택 삭제가 가능합니다.

The post How to store array or JSON obj in localstorage. appeared first on SCLOUD.

]]>
http://soya.moe/archives/864/feed 0