๐โโ๏ธ BOM(Browser Object Model)์ด๋,
๋ธ๋ผ์ฐ์ ๊ฐ์ฒด ๋ชจ๋ธ์ ๋ฌธ์ ์ด์ธ์ ๋ชจ๋ ๊ฒ์ ์ ์ดํ๊ธฐ ์ํด ๋ธ๋ผ์ฐ์ (ํธ์คํธ ํ๊ฒฝ)๊ฐ ์ ๊ณตํ๋ ์ถ๊ฐ ๊ฐ์ฒด
์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๋์๊ฐ๋ ํ๋ซํผ์ ํธ์คํธ(host)๋ผ๊ณ ๋ถ๋ฆ.
- ํธ์คํธ ํ๊ฒฝ์ด ์น ๋ธ๋ผ์ฐ์ ์ผ ๋ ์ฌ์ฉํ ์ ์๋ ๊ธฐ๋ฅ์ ๊ฐ๊ด์ ์ผ๋ก ์๋์ ๊ฐ์
- window
ใด DOM
ใด BOM (location, navigator, screen, history, ...)
ใด JavaScript (Object, Array, Function, ...)
- ์ต์๋จ์ window ๊ฐ์ฒด๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋์ ์ ์ญ ๊ฐ์ฒด์ด์ ๋ธ๋ผ์ฐ์ ์ฐฝ์ ๋๋ณํ๊ณ ์ด๋ฅผ ์ ์ดํ ์ ์๋ ๋ฉ์๋๋ฅผ ์ ๊ณต
- window ๊ฐ์ฒด๋ ์ ์ญ ๊ฐ์ฒด์ด๋ฏ๋ก ๋ฉ์๋ ํธ์ถ ์ ์๋ต ๊ฐ๋ฅ
window.open(url, name, params)
๋ฉ์๋๋ก ์ ์ฐฝ์ ์ด ์ ์์.
- url : ์ ์ฐฝ์ ๋ก๋ํ url
- name : ์ ์ฐฝ์ ์ด๋ฆ์ผ๋ก ํด๋น ์ด๋ฆ์ ๊ฐ์ง ์ฐฝ์ด ์ด๋ฏธ ์์ผ๋ฉด ๊ทธ ์์์ ์ด๋ฆฌ๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด ์ ์ฐฝ์ด ์ด๋ฆผ
- params : ์ ์ฐฝ์ ์ค์ ์ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ๋ฌธ์์ด๋ก ์ ๋ฌ
<button id="btn1">์๋ก์ด ์ฐฝ ์ด๊ธฐ</button>
<button id="btn2">์๋ก์ด ์ฐฝ ์ด๊ธฐ</button>
document.getElementById('btn1').onclick = () => {
window.open('http://www.google.com', 'popup1', 'width=1080, height=800');
};
document.getElementById('btn2').onclick = () => {
open('http://www.naver.com', 'popup2'); // ๋ฐ๋ก ํฌ๊ธฐ๋ฅผ ์ง์ ํ์ง ์์์ ๊ฒฝ์ฐ ์ ์ฐฝ
}
window.alert(message)
๋ฉ์๋๋ ํ์ธ ๋ฒํผ์ ๊ฐ์ง๋ฉฐ ๋ฉ์ธ์ง๋ฅผ ์ง์ ํ ์ ์๋ ๊ฒฝ๊ณ ๋ํ์์๋ฅผ ๋์
window.alert('์ค๋ ์ ์ฌ์ ๋ก๋ณถ์ด!')
window.confirm(message)
๋ฉ์๋๋ ํ์ธ๊ณผ ์ทจ์ ๋ ๋ฒํผ์ ๊ฐ์ง๋ฉฐ ๋ฉ์ธ์ง๋ฅผ ์ง์ ํ ์ ์๋ ๋ชจ๋ฌ ๋ํ์์๋ฅผ ๋์. 'ํ์ธ'์ ๋๋ฅผ ์ true, '์ทจ์'๋ฅผ ๋๋ฅด๊ฑฐ๋ ESC ํค๋ฅผ ๋๋ฅด๋ฉด false ๋ฐํ
const answer = window.confirm('๋ ๋์๋ ค๋ฉด ํ์ธ์ ๋๋ฅด์ธ์!')
if(answer) { // true or false๊ฐ ๋ฐํ๋๊ธฐ ๋๋ฌธ์ ์ ํ์ ๋ฐ๋ฅธ ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ฝ์์ ์ถ๋ ฅ ๊ฐ๋ฅ
console.log('์๋ ํ ์ ์ ์ถ๊ฐ!');
} else {
console.log('ํ ํ์๋ต์ง์๊ตฐ');
}
window.prompt(message, default)
๋ฉ์๋๋ ์ฌ์ฉ์๊ฐ ํ ์คํธ๋ฅผ ์ ๋ ฅํ ์ ์๋๋ก ์๋ดํ๋ ์ ํ์ ๋ฉ์ธ์ง๋ฅผ ๊ฐ๊ณ ์๋ ๋ํ ์์๋ฅผ ๋์. default๋ ํ ์คํธ ์ ๋ ฅ ํ๋์ ๊ธฐ๋ณธ์ผ๋ก ์ฑ์ ๋ฃ์ ๋ฌธ์์ด.
๋ฐํ ๊ฐ์ ํ์ธ์ ๋๋ฅผ ์ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋ฌธ์์ด์ด๋ฉฐ ์ทจ์๋ฅผ ๋๋ฅด๊ฑฐ๋ ESC ํค๋ฅผ ๋๋ฅด๋ฉด null์ด๋ค.
const likeNumber = window.prompt('๋ช ์ธ๋ถ ๋ ๋จน์๊ฑฐ์์?', 3); // 3์ ์ฐฝ ์์ default ๊ฐ
if(likeNumber) {
console.log(`๋ก๋ณถ์ด ${likeNumber}์ธ๋ถ ์ถ๊ฐ!`)
} else {
console.log('์๋์ธ์ ?')
}
๐โโ๏ธ ํ์ฌ url์ ์ฝ์ ์ ์๊ฒ ํด์ฃผ๊ณ ์๋ก์ด url๋ก ๋ณ๊ฒฝ(redirect)ํ ์ ์๊ฒ ํด์ค
/* ๋ค์ํ ๋ฐฉ์์ผ๋ก ์ฐ๊ฒฐ */
document.getElementById('btn1').onclick = () => location.assign('http://www.google.com');
document.getElementById('btn2').onclick = () => location = 'http://www.google.com';
document.getElementById('btn3').onclick = () => location.href = 'http://www.google.com';
document.getElementById('btn4').onclick = () => location.replace('http://www.google.com'); // ์ด์ ํ์ด์ง๋ฅผ ์ฐธ์กฐ ๋ถ๊ฐ (๋ค๋ก๊ฐ๊ธฐ X)
document.getElementById('btn5').onclick = () => location.reload();
document.getElementById('btn6').onclick = () => location = location;
document.getElementById('btn7').onclick = () => location.href = location.href;
๐โโ๏ธ ๋ธ๋ผ์ฐ์ ์ ์ด์์ฒด์ ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณต
for(prop in navigator) {
console.log(`${prop} : ${navigator[prop]}`); // ์ฝ์์ ์ ๋ณด ์ถ๋ ฅ
}
๐โโ๏ธ ์น ๋ธ๋ผ์ฐ์ ํ๋ฉด์ด ์๋ '์ด์์ฒด์ ํ๋ฉด'์ ์์ฑ์ ๊ฐ์ง๋ ๊ฐ์ฒด
screen.width
,screen.height
: ํ๋ฉด ๋๋น์ ๋์ดscreen.availwidth
,screen.availHeight
: ์ค์ ํ๋ฉด์์ ์ฌ์ฉ ๊ฐ๋ฅํ (์ํ ํ์์ค ๋ฑ์ ์ ์ธํ) ๋๋น์ ๋์ดscreen.colorDepth
: ์ฌ์ฉ ๊ฐ๋ฅํ ์์ ์screen.pixelDepth
: ํ ํฝ์ ๋น ๋นํธ ์
for(prop in screen) {
console.log(`${prop} : ${screen[prop]}`);
}
๐โ ๋ธ๋ผ์ฐ์ ์ ์ธ์ ๊ธฐ๋ก, ์ฆ ํ์ฌ ํ์ด์ง๋ฅผ ๋ถ๋ฌ์จ ํญ ๋๋ ํ๋ ์์ ๋ฐฉ๋ฌธ ๊ธฐ๋ก์ ์กฐ์ํ ์ ์๋ ๋ฐฉ๋ฒ์ ์ ๊ณต
length
: ํ์ฌ ํ์ด์ง๋ฅผ ํฌํจํด, ์ธ์ ๊ธฐ๋ก์ ๊ธธ์ด๋ฅผ ๋ํ๋ด๋ ์ ์ ๊ฐback()
: ๋ค๋ก ๊ฐ๊ธฐforward()
: ์์ผ๋ก ๊ฐ๊ธฐgo()
: ์ธ์๋ก ์ ๋ฌํ ๊ฐ๋งํผ ์ด๋
<button id="btn1">๋ค๋ก ๊ฐ๊ธฐ</button>
<button id="btn2">์์ผ๋ก ๊ฐ๊ธฐ</button>
<input type="number" name="page">
<button id="btn3">์
๋ ฅํ ๋งํผ ์ด๋ํ๊ธฐ</button>
console.log(history.length);
document.getElementById('btn1').onclick = () => history.back(); // ๋ค๋ก๊ฐ๊ธฐ
document.getElementById('btn2').onclick = () => history.forward(); // ์์ผ๋ก ๊ฐ๊ธฐ
document.getElementById('btn3').onclick = () => {
let page = document.querySelector('input[name=page]').value;
history.go(page); // ์
๋ ฅํ ๋งํผ ์ด๋ํ๊ธฐ
};