ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

๐Ÿงฐ 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 ์‚ฌ์šฉ ๊ถŒ์žฅ
๋Œ“๊ธ€