클래스 이용해서 만든 것
let object = {
key: "값",
key: "값"
}
key: value 형태로 이뤄져있다.
객체 안에는 변수, 배열, 함수, 객체 등을 다 넣을 수 있다.
{} 괄호 사용
let 객체 = {
className: "Dclass",
time: "09~18",
s_num : 26,
능력단위: ['디자인UI', 'SQL응용', 'SQL활용'],
하는일:function(){
alert('객체');
}
}
// 객체값 불러오기, 함수 실행하기
console.log(객체);
console.log(객체.className);
console.log(객체.능련단위[1]);
객체.하는일();
// 기존 객체에 삽입
객체['branch'] = "강남";
let dt = new Date();dt.getFullYear();년월일을 더하거나 뺄때는 아래 set내장함수 사용해야한다.
(이유: 37일, -1일이 나올수도 있음)
dt3 = new Date(dt3.setDate(dt3.getDate()+30))let dt = new Date();태그에 속성처럼 <data-데이터셋명="값"> 넣고 js에서 <dataset.데이터셋명>으로 넣어놓은 값을 가져올 수 있다.
<div id="box" data-count="0"></div>
let box = document.getElementById('box');
console.log(box.dataset.count);
location.reload();location.href="http://www.naver.com";location.replace("http://www.naver.com");screen.widthscreen.heightscreen.availWidthscreen.availHeightdocument.body.clientWidthdocument.body.clientHeightwindow.innerWidthwindow.innerHeightwindow.outerWidthwindow.outerHeightwindow.screenLeftwindow.screenTopdocument.getElementById('box').offsetTopdocument.getElementById('box').offsetLeftdocument.getElementById('box').offsetWidthnavigator.userAgent.toLowerCase();navigator.userAgent : 브라우저 정보
navigator.cookieEnabled : 쿠키 사용여부
navigator.platform : 실행되는 플랫폼
navigator.appCodeName : 브라우저 코드명
navigator.appName : 브라우저 이름
navigator.product : 사용하고 있는 엔진명
let userAgent2 = window.navigator.userAgent.toLowerCase();
let sp = ['android', 'iphone', 'ipad'];
for(let i = 0; i < sp.length; i++){
if(userAgent2.match(sp[i])){
console.log(`${i}: ${sp[i]}`);
location.replace('https://m.naver.com');
break;
}
}
-> 모바일인지 확인해서 모바일페이지로 넘기는 코드
모바일 페이지 넘길 때 replace(히스토리 안남음)랑 href(히스토리 남음) 둘 중 아무거나 써도 상관없다.
정석은 히스토리를 안남기는 것이니 replace를 쓰는 것이긴 하나 장치 체크해서 바로 url바꾸기 때문에 href써도 뒤로가기 눌러도 pc페이지로 넘어가지 않는다.
navigator.maxTouchPoints : 기기에 터치를 감지하는 부분이 있는지 없는지 판별
if(navigator.maxTouchPoints >= 1){
// 스마트 기기라는 뜻
}else{
// 일반 기기
}
팝업창 생성 / 삭제(탭으로 동작)
window.open() - 빈 탭 띄움
window.open("https://www.naver.com"); - 새탭에서 띄움
window.open("https://www.naver.com", '_self'); 현재탭에서 띄움
window.open("./210.window_자식창.html", '_blank', 'width=300px, height=500px, left = 1000px, top=500px'); _blank를 넣어놓고(self는 안됨) 크기(width, height)와 위치(left, top)를 지정해줄 수 있다.
요소.close();앞에서 자식창 열 때 let child = window.open("./210.window_자식창.html", '_blank', 'width=300px, height=500px, left = 1000px, top=500px' 이렇게 저장하고 child.close()하면 된다.
var는 function끼리에서는 전역변수처럼 사용이 불가해서 자식창 생성할 때 var로 생성해도 뒤에서 닫는 메서드에서 해당 창이 인식이 안된다.
function empty_open(){
window.open();
}
function blank_open(){
window.open("./210.window_자식창.html");
}
function self_open(){
window.open("./210.window_자식창.html", '_self');
}
let child;
function child_open(){
// 부모창 위치
let parent_pos_x = window.screenLeft;
let parent_pos_y = window.screenTop;
// 부모창 크기
let parent_x = window.innerWidth / 2;
let parent_y = window.innerHeight / 2;
// 자식창 크기
let child_x = 300;
let child_y = 500;
// 자식창 위치
let child_pos_x = parent_pos_x + parent_x - (child_x / 2);
let child_pos_y = parent_pos_y + parent_y - (child_y / 2);
// 자식창을 이미 켜놨다면 닫고 다시 생성할 코드
if(child != null){
child_close();
}
child = window.open("./210.window_자식창.html", '_blank', 'width='+child_x+'px, height='+child_y+'px, left = '+child_pos_x+'px, top='+child_pos_y+'px');
}
function child_close(){
child.close();
}
창에서 창으로 정보전달
정보전달은 서버에 올려놔야만 확인 가능함. 보안상의 이유로 로컬에서는 막힘.
부모 > 자식 값 보내기
자식창.document.getElementById('') 해서 해당 값을 가져올 수 있다.
let child;
child = window.open("./210.window_자식창.html", '_blank', 'width='+child_x+'px, height='+child_y+'px, left = '+child_pos_x+'px, top='+child_pos_y+'px');
child.document.getElementById('child_txt').value = parent_txt.value;
자식창
//부모 js
let parent_txt = document.getElementById('parent_txt');
child.get_from_parent(parent_txt);
//자식 js
function get_from_parent(txt){
document.getElementById('child_txt').value = txt;
}
자식 > 부모 값 보내기
opener.get_from_child(document.getElementById('child_txt').value);let rand = Math.random();Math.floor(Math.random() * (max - min + 1)) + min > 최대값 포함Math.floor(Math.random() * (max - min)) + min > 최소값 포함Math.random() * (자리수)Math.floor(Math.random() * 10) -> 0~9Math.ceil(Math.random() * 10) -> 1~10Math.floor(10.948) -> 10Math.floor(10.948) -> 11Math.max(10, 9, 11, 5) -> 11Math.max(10, 9, 11, 5) -> 5Math.abs(1) -> 1Math.abs(-1) -> 1Math.pow(2, 3) -> 2의 3승 -> 8Math.sqrt(4) -> 4 루트 -> 2