Browser Object Mode이란 브라우저를 객체를 통해 제어하는 방법이다.
이중 객체란 윈도우(전역 객체)에 포함된 프로퍼티와 메서드들을 말한다.
<!DOCTYPE html>
<html>
<body>
<input type="button" value="alert" onclick="alert('hello world');" />
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input type="button" value="confirm" onclick="func_confirm()" />
<script>
function func_confirm(){
if(confirm('ok?')){
alert('ok');
} else {
alert('cancel');
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input type="button" value="prompt" onclick="func_prompt()" />
<script>
function func_prompt(){
if(prompt('id?') === 'egoing'){
alert('welcome');
} else {
alert('fail');
}
}
</script>
</body>
</html>
: 현재 위치의 주소를 찾아낼 수 있는 방법이다.
location.toString()
location.href
console.log(location)
arlert(location)
: 현재 문서의 다양한 정보를 알아낼 수 있는 프로퍼티를 사용할 수 있다.
console.log(location.protocol) // output: http//
location.href = 'url'; // url값으로 이동시켜준다.
location = 'url'; // 위와 동일
locarion.reload // 리로드 시킨다.
console.dir(navigator);
: 브라우저에 내장되어 있는 키를 사용하고, 없다면 기능테스트를 통해 해결한다.
if (!Object.keys) {
Object.keys = (function () {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
var result = [], prop, i;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}