ํฐ์คํ ๋ฆฌ ๋ทฐ
๋ฐ์ํ
๐งฐ JavaScript ์ ๋ฆฌ(์นํธ์ํธ)
ํ๋ ๋ธ๋ผ์ฐ์ ํ์ค ์ค์ฌ์ผ๋ก ์ฌ์ ๋ฆฌ. ์ค๋๋ IE/๋ท์ค์ผ์ดํ ์ ์ฉ ํญ๋ชฉ์ (๋ ๊ฑฐ์) ํ๊ธฐ.
1) ๊ธฐ๋ณธ ๊ฐ๋
๐น ๊ฐ์ฒด(Object)
HTML ๋ฌธ์, ๋ธ๋ผ์ฐ์ ์ฐฝ, ๋ ์ง/์๊ฐ ๋ฑ.
- ๋ฉ์๋: ๊ฐ์ฒด๊ฐ ์ํํ๋ ๋์
- ์์ฑ: ๊ฐ์ฒด์ ์ํ/ํน์ง
์์ฃผ ์ฐ๋ ๊ฐ์ฒด:
window(๋ธ๋ผ์ฐ์ ์ฐฝ)document(ํ์ฌ ๋ฌธ์)location(URL/์ด๋)history(ํ์ ๊ธฐ๋ก)navigator(๋ธ๋ผ์ฐ์ /ํ๋ฌ๊ทธ์ธ ์ ๋ณด)screen(ํ๋ฉด ์ ๋ณด)Date(๋ ์ง/์๊ฐ)
const now = new Date(); // ํ์ฌ ์๊ฐ
parent.location = 'index.html' // ์์์ฐฝ URL ์ด๋
2) ๋ฉ์๋(Method)
๋ธ๋ผ์ฐ์ ๋ํ์์
window.alert('๋ฉ์์ง'); // ๊ฒฝ๊ณ
const v = prompt('์
๋ ฅ', '์ด๊ธฐ๊ฐ'); // ์
๋ ฅ
const ok = confirm('ํ์ธ?'); // ํ์ธ/์ทจ์
window.print(); // ์ธ์
๋ฌธ์ ์ถ๋ ฅ (๋ ๊ฑฐ์)
document.write('ํ
์คํธ'); // ๋์ DOM ๊ถ์ฅ (์๋ ์ฐธ๊ณ )
์ํ/์ซ์
parseInt('10.9', 10); // 10
parseFloat('10.9'); // 10.9
Math.ceil(1.2); // 2 (์ฌ๋ฆผ)
Math.floor(1.8); // 1 (๋ด๋ฆผ)
Math.round(1.5); // 2 (๋ฐ์ฌ๋ฆผ)
Math.abs(-3); // 3
Math.pow(4,2); // 16
Math.PI; // ์์ฃผ์จ
Math.floor(Math.random()*6)+1; // 1~6 ๋์
3) ์ปฌ๋ ์ /๋ฌธ์์ด ์ ํธ
๋ฐฐ์ด
const arr = ['1','2','5','4','3'];
arr.length; // ๊ธธ์ด
arr.sort(); // ์ ๋ ฌ (๋ฌธ์์ด ๊ธฐ์ค)
arr.reverse(); // ์ญ์
arr.concat([9]) // ๋ณํฉ
arr.slice(1,3); // ์ฌ๋ผ์ด์ค
arr.join(','); // "1,2,5,4,3"
๋ฌธ์์ด
const s = 'ABC';
s.length; // ๊ธธ์ด
s.charAt(0); // 'A'
s.charCodeAt(0); // 65
String.fromCharCode(65,66,67); // "ABC"
'hello'.toUpperCase(); // "HELLO"
'hello'.toLowerCase(); // "hello"
'ABCDE'.indexOf('C'); // 2
'ABCDABC'.lastIndexOf('ABC'); // 4
'abcdef'.substring(2,5); // 'cde'
'abcdef'.substr(2,3); // 'cde' *(๋นํ์ค)*
'1997, 1998'.replace('1997','2000'); // ์ฒซ ํญ๋ชฉ๋ง
'1,2,3'.split(','); // ['1','2','3']
4) ์ ์ด๋ฌธ/๊ฒ์ฌ
if (cond) { ... } else if (...) { ... } else { ... }
switch (v) {
case '1': ...; break;
case '2': ...; break;
default : ...; break;
}
for (let i=0;i<5;i++) { ... }
while (i<5) { ... }
isNaN(value); // ์ซ์๋ฉด false, ๋ฌธ์๋ true
5) Date (๋ ์ง/์๊ฐ)
const d1 = new Date(); // now
const d2 = new Date(2000, 0, 31, 15, 35, 20); // ์ 0=1์
d1.getFullYear(); d1.getMonth(); d1.getDate();
d1.getDay(); d1.getHours(); d1.getMinutes();
d1.getSeconds(); d1.getMilliseconds();
d1.setFullYear(2025); d1.setMonth(10); d1.setDate(3);
d1.valueOf(); // 1970-01-01๋ถํฐ ms
6) ์๋์ฐ/๋ฌธ์/๋ค๋น๊ฒ์ด์
// location
location.href = 'https://example.com';
location.reload();
location.replace('/login'); // ํ์คํ ๋ฆฌ ๋จ๊ธฐ์ง ์์
// history
history.back(); history.forward(); history.go(-1);
// navigator (๋ธ๋ผ์ฐ์ ์ ๋ณด)
navigator.userAgent;
// screen
screen.width; screen.height; screen.colorDepth;
7) DOM (ํ์ค)
<h1 id="heading1">์ ๋ชฉ</h1>
const el = document.getElementById('heading1');
el.style.fontFamily = 'serif';
const tds = document.getElementsByTagName('td'); // HTMLCollection
document.documentElement.tagName; // "HTML"
// ๋
ธ๋ ํ์
el.firstChild; el.lastChild;
el.parentNode; el.nextSibling; el.previousSibling;
// ์์ฑ/์ฝ์
const h1 = document.createElement('h1');
const text = document.createTextNode('์ ์ ๋ชฉ');
h1.appendChild(text);
document.body.appendChild(h1);
// ์์ฑ
h1.setAttribute('align','center');
h1.getAttribute('align');
h1.removeAttribute('align');
document.all,layer/ilayer,outerHTML์ง์ ์กฐ์ ๋ฑ์ (๋ ๊ฑฐ์).
ํ์ค DOM API ์ฌ์ฉ ๊ถ์ฅ.
8) ํผ/์ด๋ฏธ์ง/๋งํฌ (์ผ๋ถ ๋ ๊ฑฐ์ ํฌํจ)
document.forms[0].elements['name'].value;
document.images['logo'].src = 'logo.png';
document.links[0].onclick = () => { ... };
// Select ์ต์
์กฐ์
const opt = new Option('ํ์ํ
์คํธ','๊ฐ');
document.form1.selectObject.options[0] = opt;
document.form1.selectObject.options[0] = null; // ์ ๊ฑฐ
const v = document.form1.selectObject
.options[document.form1.selectObject.selectedIndex].value;
9) ์ฐฝ ์ ์ด / ๊ด๊ณ
// ์ ์ฐฝ ์ด๊ธฐ
const w = window.open(
'/popup.html',
'targetName',
'width=600,height=400,left=100,top=100,resizable=yes,scrollbars=yes,status=yes,toolbar=no'
);
w.resizeTo(350,200);
w.moveTo(100,400);
w.resizeBy(100,200);
w.moveBy(20,50);
window.close();
// ์ฐฝ ๊ด๊ณ
window.name;
window.opener; // ๋๋ฅผ ์ฐ ์ฐฝ
window.parent; // ๋ถ๋ชจ ํ๋ ์
window.top; // ์ต์์ ์ฐฝ
10) ์ด๋ฒคํธ ํธ๋ค๋ฌ
HTML ์์ฑํ(
onclick="...") ๋์ addEventListener ๊ถ์ฅ.
element.addEventListener('click', handler);
element.addEventListener('mouseover', handler);
element.addEventListener('change', handler);
// ํฌ์ปค์ค: focus/blur, ๋ก๋: load, ์ธ๋ก๋: beforeunload/unload ๋ฑ
์ด๋ฒคํธ ๊ฐ์ฒด ์ผ๋ถ
e.type;
e.currentTarget;
e.bubbles; e.cancelable;
e.clientX; e.clientY; // ๋ทฐํฌํธ ์ขํ
e.screenX; e.screenY; // ์คํฌ๋ฆฐ ์ขํ
e.altKey; e.ctrlKey; e.shiftKey; e.metaKey;
์๋ฌธ์ ์๋
on_click,on_mouseOver๋ฑ ์ธ๋์ค์ฝ์ด ํ๊ธฐ๋ (๋ ๊ฑฐ์).
ํ์ค์ ์๋ฌธ์onclick,onmouseover๋๋addEventListener('mouseover', ...).
11) ์ ๊ท์(Regex) ๋ฉ๋ชจ
// ๋ฆฌํฐ๋ด๊ณผ ํ๋๊ทธ
const re = /\d-\d{3}-\d{3}-\d{4}/gi;
// ๋ฉํ
// \d ์ซ์, \D ๋น์ซ์, \w ๋จ์ด๋ฌธ์, \W ๋น๋จ์ด, \s ๊ณต๋ฐฑ, \S ๋น๊ณต๋ฐฑ, . ์์๋ฌธ์
// ^ ์์, $ ๋, \b ๋จ์ด๊ฒฝ๊ณ
// ์๋์: {n}, {n,}, {n,m}, ?, +, *
'1999, 2000'.match(/\d{4}/g); // ['1999','2000']
'1999'.replace(/(\d{4})/g, 'year $1'); // "year 1999"
12) ํ์ด๋จธ
const id1 = setTimeout(() => alert('3์ด ํ'), 3000);
clearTimeout(id1);
const id2 = setInterval(tick, 5000);
clearInterval(id2);
13) ์ฟ ํค (๊ฐ๋จ ์์)
document.cookie = 'UserName=ํ๊ธธ๋; expires=Tue, 28 Dec 2030 00:00:00 GMT; path=/';
const expires = new Date();
expires.setMonth(expires.getMonth()+6);
document.cookie = `UserName=ํํํ; expires=${expires.toUTCString()}; path=/mystore`;
escape/unescape(๋ ๊ฑฐ์) โencodeURIComponent์ฌ์ฉ ๊ถ์ฅ.
๋ณด์/ํธ์์ฑ ์ํดlocalStorage๋๋ ์๋ฒ ์ธ์ /ํ ํฐ ๊ณ ๋ ค.
14) ๋๋ฒ๊ทธ ์ ํธ(์๋ฌธ ์ ๊ณต, IE ์ ์ฉ API ํฌํจ)
// ๋ชจ๋ ํ๋กํผํฐ/๊ฐ ๋์ด (IE: clipboardData ์ฌ์ฉ, ํ๋ ๋ธ๋ผ์ฐ์ ๋ navigator.clipboard ๊ถ์ฅ)
function debug(node) {
let out = '';
for (const key in node) out += `${key} : ${node[key]}\n`;
if (window.clipboardData) window.clipboardData.setData('Text', out);
alert('๋ณต์ฌ์๋ฃ');
}
// ํน์ element outerHTML ๋ณต์ฌ (IE ์ ์ฉ)
function debug2(id) {
if (window.clipboardData) {
window.clipboardData.setData('Text', document.all[id].outerHTML);
alert('๋ณต์ฌ์๋ฃ');
}
}
15) ๋ ๊ฑฐ์/์ฃผ์ ํญ๋ชฉ ์์ฝ
document.all,layer/ilayer,bgColor,status, ActiveX<OBJECT>,
์ธ๋์ค์ฝ์ด ์ด๋ฒคํธ๋ช (on_mouseover) โ ํ๋ ํ์ค ์๋- ๋์ ๋ฌธ์ ๊ตฌ์ฑ์ DOM API ๊ถ์ฅ (
createElement,appendChild,innerHTML) - ๋ฌธ์์ด
substr๋ ๋นํ์ค.substring/slice์ฌ์ฉ ๊ถ์ฅ
'Programming > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์๋ฐ์คํฌ๋ฆฝํธ ์ผ์ ์๊ฐ ์์ ๋์ํ๊ฒ ํ๊ธฐ (0) | 2020.02.05 |
|---|---|
| javascript this์ ์ดํด (0) | 2014.08.25 |
| ์ด๋ฏธ์งํ๊ทธ ์ค๋ฅ์ฒ๋ฆฌ (onerror) (0) | 2014.01.16 |
| JavaScript ์คํ์ผ ๊ฐ์ด๋ (0) | 2013.12.26 |
| ์๋ฐ์คํฌ๋ฆฝํธ ํค์ฝ๋ ์ ๋ฆฌ (0) | 2013.11.18 |
๋๊ธ
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- oracle ์ค๋ฐ๊ฟ
- ๋ก๊ทธ์ด๊ธฐํ
- ์ค๋ผํด ์ธ์
- internal error
- 0์ผ๋ก
- Java
- ์ค๋ผํด
- Oracle
- ์๊ฐ์ง์
- jQuery
- ๋์ฒด์ด
- ํธ์ํฐ ํ์๋ผ์ธ ์์ ฏ
- IE๋์ธ์ ์์ด์ง๋
- ์์๋ฅผ
- ์๊ฐ๋น๊ต
- jeus
- ์ค๋ผํด ๋ณต๊ตฌ
- ์ค๋ผํด ๋ฝ
- webtob
- javascript
- ์๋ฐ์คํฌ๋ฆฝํธ
- IE๋ชจ๋
- ์นํฌ๋น
- html
- ํธ์ํฐ ์์ ฏ
- this
- ๋ช ๋ น์ด
- ์ฌ์ฉ์์ญ์
- ๋ฝ๊ฑธ๋ฆผ
- lock
| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
๊ธ ๋ณด๊ดํจ