[Day7] JS 7 [11/28]
js ํจ์ ํด๋ก์ ( closures ) *** : https://www.youtube.com/watch?v=tpl2oXQkGZs
js ๋ณ์
1) ์ง์ญ๋ณ์ - funtion scope, block scope
ํธ์ถ ์์ฑ-> ์ข ๋ฃ ์ญ์
2) ์ ์ญ๋ณ์ - global scope
๋ค๋ฅธ ํ์ด์ง๋ก ์ด๋, ์ฐฝ ๋ซํ ๋ , ์ฆ ํ์ด์ง ์ ์ง ๋ ๋๊น์ง..
js ํด๋ก์ ( ํ์ )
- ์ ์ญ๋ณ์๋ฅผ ๋ก์ปฌ(์ง์ญ)์ ์ ์ธํด์ ๋น๊ณต๊ฐ์ ์ผ๋ก ๋ง๋ค ์ ์๋ค.
- ์๋ํ ๊ฐ๋ฅ
<1> js ์ค์ฒฉ ํจ์ ์ดํด.
function add(){
let counter = 0; // ์ง์ญ๋ณ์ -> js ํด๋ก์
// counter += 1 ; // 1 ์ฆ๊ฐ
function plus(){ // js ์ค์ฒฉ ํจ์๋ฅผ ์ง์ํ๋ค.
// ์ค์ฒฉํจ์(plus) ์์์ ์์(๋ถ๋ชจ)ํจ์(add)์ ์ง์ญ๋ณ์์ ์ ๊ทผ ๊ฐ๋ฅ
counter += 1;
}
plus(); // ์ค์ฒฉํจ์ ํธ์ถ ์ฝ๋ฉ.
return counter;
} // add
$("#demo").html( add() );
<2> plus() ์ค์ฒฉํจ์๋ฅผ add() ํจ์๋ฅผ ํตํ์ง ์๊ณ ์ธ๋ถ์์ ์ ๊ทผํ ์ ์๋ค๋ฉด add() ํจ์ ์์ let counter = 0; // ์ง์ญ๋ณ์ ์ด๊ธฐํ ํ๋ ์ฝ๋ฉ์ ํ ๋ฒ๋ง ์คํ๋์ด ์ด๊ธฐํ ๋๊ณ
[ js ํด๋ก์ ]
- 1) js ์ค์ฒฉํจ์
- 2) add() X, plus() ํธ์ถ -> ์์ฒด ํธ์ถ ํจ์
js ํด๋ก์ = ์ค์ฒฉํจ์ + ์์ฒดํธ์ถ ํจ์
//์์ฒด ํธ์ถ ํจ์
var plus = (function (){ // ์์ฒดํธ์ถ ํจ์ - 1๋ฒ ์คํ
let counter = 0; // ์ง์ญ๋ณ์ -> ์ ์ญ๋ณ์ ( ํ์ )
return function (){ // ํจ์์ ๋ฆฌํด๊ฐ์ด ์ค์ฒฉํจ์
// ๋ถ๋ชจํจ์์ ์ง์ญ๋ณ์๋ฅผ ์ ๊ทผํ ์ ์๋ค.
counter += 1;
return counter;
};
})();
$("button").click(function() {
$("#demo").html( plus() );
})
- ์ ? counter ์ง์ญ๋ณ์๋ฅผ ์ ์ญ๋ณ์๋ก ์ ์ธํ๋ฉด ์ฝ๊ฒ ํด๊ฒฐ ...-> js ํด๋ก์
- js ํด๋ก์ ๋ ๋ถ๋ชจํจ์๊ฐ ๋ซํ ํ์๋ ๋ถ๋ชจ ๋ฒ์ ์์ธ์คํ ์ ์๋ ํจ์
ํด๋ก์ ํจ์ ์ฌ์ฉํ๋ ์ด์
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
<button>0</button>
ํด๋ก์ X
// ๊ฐ ๋ฒํผ 20๊ฐ + counter 20 ๊ฐ ์ ์ญ ์ ์ธ
var count1 = 0;
var count2 = 0;
:
var count20 = 0;
var counts = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
// var counts = [];
// for( : i<20 ) counts.push(0)
$("button").click(function() {
// jquery method : index()
var idx = $(this).index();
$(this).html( ++counts[idx] );
});
ํด๋ก์ O
// jquery
$("button").click(
(function() { // ์์ฒด ํธ์ถ ํจ์ + ์คํ
let count = 0; //
return function (){ // ์ค์ฒฉ ์ ์ธ
event.srcElement.innerHTML = ++count;
};
})()
);
// js
var btns = document.getElementsByTagName("button");
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = ( function (){ // ์์ฒด ํธ์ถ ํจ์
let counter = 0;
return function(){ // ์ค์ฒฉํจ์
event.srcElement.innerHTML = ++counter;
};
})();
};
์์ )
var counterObject = (function (){ // ์์ฒด ํธ์ถ ํจ์
let counter = 0; // ์ง์ญ๋ณ์ -> ์ ์ญ๋ณ์
function changeBy( value ){ // ์ค์ฒฉํจ์
counter += value;
}
return {// js ๊ฐ์ฒด ๋ฆฌํด
increment:function (){ changeBy(1)},
decrement:function (){ changeBy(-1)},
value:function (){ return counter;}
};
})();
console.log( counterObject.value() ); // 0
counterObject.increment();
counterObject.increment();
counterObject.increment();
console.log( counterObject.value() ); // 3
counterObject.decrement();
console.log( counterObject.value() ); // 2
์์ 2)
var btns = document.querySelectorAll(".myProgress button");
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = function (){
// this == event.srcElement == ํด๋ฆญ๋ ๋ฒํผ
var btn = event.srcElement;
var width = 1; // let counter = 0 ์ง์ญ๋ณ์ -> ์ ์ญ๋ณ์ ( ํด๋ก์ )
var timer = setInterval(function() { // ์ค์ฒฉํจ์
if( width < 100){
width++;
// <div id="myBar1" class="myBar">1%</div>
btn.previousElementSibling.style.width = width +"%";
btn.previousElementSibling.innerHTML = width +"%";
}else{
clearInterval(timer);
}
}, 30);
}
} // for
$(".myProgress button").click( function (){
var btn = $(this);
var width = 1;
var timer = setInterval(function() { // ์ค์ฒฉํจ์
if( width < 100){
width++;
// jquery method : prev(), next()
// js " : previousElementSibling, nextElementSibling
btn.prev().css("width", width+"%");
btn.prev().html( width +"%" );
}else{
clearInterval(timer);
}
}, 30);
});
dragdrop
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<div id="draggable" class="ui-widget-content">
<p>Drag me around</p>
</div>
// jquery-ui.js ์ธ๋ถ js ํ์ผ
$("#draggable").draggable();
$("h3").draggable();
์์ )
.myDiv{
background-color: #f1f1f1;
text-align: center;
border:1px solid #d3d3d3;
position: absolute;
z-index:9;
}
.myDivHeader{
padding: 10px;
cursor: move;
z-index:10;
background-color: #2396F3;
color:#fff;
}
<!-- #myDiv1.myDiv>#myDiv1Header.myDivHeader -->
<div id="myDiv1" class="myDiv">
<div id="myDiv1Header" class="myDivHeader">Click Here</div>
<!-- p*3>lorem1 -->
<p>Lorem.</p>
<p>Nesciunt.</p>
<p>Reiciendis.</p>
</div>
<div id="myDiv2" class="myDiv">
<div id="myDiv2Header" class="myDivHeader">Click Here</div>
<!-- p*3>lorem1 -->
<p>Lorem.</p>
<p>Nesciunt.</p>
<p>Reiciendis.</p>
</div>
function draggable( elemt){
var elemtX = 0, elemtY = 0;
var moveX = 0, moveY = 0; // ์ด๋ํ x,y ์ขํ
// "myDiv1"
if( document.getElementById( elemt.id +"Header" ) ){
document.getElementById( elemt.id +"Header" ).onmousedown = dragMouseDown; // ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ฑ๋ก
}
// ์ค์ฒฉํจ์
function dragMouseDown( e ){
e = window.event || e; // event
// console.log( e.clientX + " / " + e.clientY );
// divHeader ์์์ ๋ง์ฐ์ค๋ค์ด ํ ๋ ์ X,Y ์ขํ ์ ์ฅ
elemtX = e.clientX ;
elemtY = e.clientY ;
document.onmousemove = elementDrag ;
document.onmouseup = closeDragElement;
}
// mousemove
function elementDrag(){
e = window.event || e; // event
// console.log( e.clientX + " / " + e.clientY );
moveX = elemtX - e.clientX; // - ์์
moveY = elemtY - e.clientY;
// elemt.left; -> "10px" ๋ฌธ์์ด
// elemt.offsetLeft; -> 10
elemt.style.left = ( elemt.offsetLeft - moveX )+"px";
elemt.style.top = ( elemt.offsetTop - moveY )+"px";
// ์์ง์ธ ์์น๊ฐ ๋ค์ ์์์ ์์ ์์น.
elemtX = e.clientX;
elemtY = e.clientY;
}
// mouseup
function closeDragElement(){
// ๋ง์ฐ์ค ์ด๋ฒคํธ ์ ๊ฑฐ
document.onmousemove = null ;
document.onmouseup = closeDragElement;
}
} // draggable
//
draggable( document.getElementById("myDiv1") );
draggable( document.getElementById("myDiv2") );
์์ 2)
#container{
width:400px;
height:400px;
position:relative;
background-color:yellow;
}
#animate{
width:50px;
height:50px;
position:absolute;
background-color:red;
border-radius: 50%;
}
<button onclick="myMove();">Click Me</button>
<br>
<br>
<div id="container">
<div id="animate"></div>
</div>
var no = 1;
function make(){
var newCircle = document.createElement("div");
newCircle.className = "animate";
newCircle.setAttribute("id", "animate" + no );
document.getElementById("container").appendChild(newCircle);
myMove( "animate" + no );
no++;
}
function myMove( id ){
// ํด๋ก์
var elem = document.getElementById( id );
var posX = 0, posY = 0;
var x = parseInt( Math.random()*8 )+2, y = parseInt( Math.random()*8 )+2;
var timer = setInterval(function() { //์ค์ฒฉํจ์
if ( posY >= 350 || posY <= 0 ) y *= -1; // y ๋ฐฉํฅ ๋ฐ๊พผ๋ค.
if ( posX >= 350 || posX <= 0 ) x *= -1;
posX += x; // 6
posY += y; // 9
if( posX < 0 ) posX = 0;
else if( posX > 350 ) posX = 350;
if( posY < 0 ) posY = 0;
else if( posY > 350 ) posY = 350;
elem.style.left = posX+"px";
elem.style.top = posY+"px";
}, 10);
}
์ปฌ๋ ์
1. arguments
2. options
3. forms
-----------------
4. elements
5. images
6. links
elements
<form id="form1" name="form1" action="">
Name : <input type="text" name="name" value="admin" />
<br>
Age : <input type="text" name="age" value="20" />
<br>
Email : <input type="email" name="email" value="admin@naver.com" />
<button type="button" value="ok">ํ์ธ</button>
</form>
<p id="demo"></p>
$("button").click(function() {
var form1 = document.forms["form1"];
var cnt = form1.elements.length;
// alert( cnt );
for (var i = 0; i < cnt; i++) {
$("#demo").html(
$("#demo").html() + "<br>" + form1.elements[i].value
);
}//
});
๋ค์ด๋ฒ ํ๊ทธ(images, links)
// naver ๋ฉ์ธํ์ด์ง์ ์ด๋ฏธ์งํ๊ทธ
var cnt = document.images.length;
for (var i = 0; i < cnt; i++) {
var src = document.images[i].src;
document.write( (i+1) +" : " + src + "<br>" );
document.images[i].src = "#";
} // for
// naver ๋ฉ์ธํ์ด์ง์ ๋งํฌํ๊ทธ
var cnt = document.links.length;
// alert( cnt ); // 430
for (var i = 0; i < cnt; i++) {
var href = document.links[i].href;
var text = document.links[i].text;
document.write( (i+1) +" : " + href + " / " + text +"<br>" );
} // for
js BOM
1. Browser Object Model ( ๋ธ๋ผ์ฐ์ ๊ฐ์ฒด ๋ชจ๋ธ ) ์ฝ์ด
2. BOM ์ข ๋ฅ
- window ๊ฐ์ฒด๋ฅผ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ์ง์ํ๋ค.
- ์ต์์ ๋ถ๋ชจ ๊ฐ์ฒด
- ์ ์ญ๋ณ์, ํจ์ ์ ์ธํ๋ฉด ์๋์ผ๋ก window ๊ฐ์ฒด์ ๊ตฌ์ฑ์์ด ๋๋ค.
- ๋ธ๋ผ์ฐ์ ์ฐฝ(์๋์ฐ)์ ๋ํ๋ธ๋ค.
1) window
// ex04.html, ex04_02.html ์ฐํธ๋ฒํธ ๊ฒ์.~
2) screen
3) history
4) navigator
5) location
3. BOM- ๋ธ๋ผ์ฐ์ ์ํธ ์์ฉ / ์ ๋ณด ์ป+์ค
var msg = "";
function test(){
}
// alert( window.msg );
// window.test();
// ๋๊ตฌ๋ชจ์, ์คํฌ๋กค๋ง๋ ๋ฑ์ ํฌํจํ์ง ์๋ ๋๋น/๋์ด
var ww = window.innerWidth; // ์ฐฝ ๋๋น
var wh = window.innerHeight; // ์ฐฝ ๋์ด
console.log( ww + " / " + wh );
var cw = document.body.clientWidth; // ์ฐฝ ๋์ด
var ch = document.body.clientHeight; // ์ฐฝ ๋์ด
console.log( cw + " / " + ch );
// ex03_02.html, ex03_03.html
// ์์ฐฝ ์ด๊ธฐ window.open()
// ํ์ฌ์ฐฝ ๋ซ๊ธฐ window.close()
// ํ์ฌ์ฐฝ ์ด๋ window.moveTo(), window.moveBy()
// ํ์ฌ์ฐฝ ํฌ๊ธฐ ์กฐ์ window.resizeTo(), window.resizeBy();
์์ฐฝ์ด๊ธฐ, ์ฐฝ๋ซ๊ธฐ
<button onclick="windowOpen();">์ ์ฐฝ ์ด๊ธฐ</button>
<button onclick="windowClose();">์ฐฝ ๋ซ๊ธฐ</button>
var newWin;
function windowOpen(){
newWin = window.open("ex03_03.html", "", "width=600,height=500,top=200");
}
function windowClose(){
newWin.close();
// self.close()
}
์ ๋์์น ์๋์์น ์ ๋ํฌ๊ธฐ ์๋ํฌ๊ธฐ
<table border="1" style="width:100%">
<tr>
<td>์ ๋์์น</td>
<td onclick="fn_moveto()">moveTo()</td>
</tr>
<tr>
<td>์๋์์น</td>
<td onclick="fn_moveby()">moveBy()</td>
</tr>
<tr>
<td>์ ๋ํฌ๊ธฐ</td>
<td onclick="fn_resizeto()">resizeTo</td>
</tr>
<tr>
<td>์๋ํฌ๊ธฐ</td>
<td onclick="fn_resizeby()">resizeBy</td>
</tr>
<tr>
<td><button onclick="win_shake();">์ฐฝ ํ๋ค๊ธฐ</button></td>
<td><button onclick="win_close();">์ฐฝ ๋ซ๊ธฐ</button></td>
</tr>
</table>
function fn_moveto(){
window.moveTo(10,10); // ์ ๋ ์์น
}
function fn_moveby(){
window.moveBy(10,10); // ์๋ ์์น
}
function fn_resizeto(){ // ์ ๋ํฌ๊ธฐ
window.resizeTo(300,300);
}
function fn_resizeby(){ // ์๋ํฌ๊ธฐ
window.resizeBy(10,10);
}
function win_shake(){
// moveBy(??,??)
}
function win_close(){
self.close();
}
2) screen
3) history
4) navigator
5) location
<table border="1" style="width: 500px;margin: 0 auto">
<tr>
<td>์ฐํธ๋ฒํธ</td>
<td>
<!-- ์ฐํธ๋ฒํธ input ํ๊ทธ๋ zip1/zip2 readonly ์์ฑ์ ์ํด ์ฝ๊ธฐ ์ ์ฉ -->
<input type="text" id="zip1" name="zip1" size="3" readonly="readonly" />-
<input type="text" id="zip2" name="zip2" size="3" readonly="readonly"/>
<input type="button" value="์ฐํธ๋ฒํธ ๊ฒ์" onclick="search_zipcode();" />
</td>
</tr>
<tr>
<td>์ฃผ์</td>
<td><input type="text" id="addr1" name="addr1" style="width:90%" /></td>
</tr>
<tr>
<td>์์ธ์ฃผ์</td>
<td><input type="text" id="addr2" name="addr2" style="width:90%" /></td>
</tr>
</table>
var zipWin; // undefined "" null "false" => false
function search_zipcode(){
// ์์ฐฝ์ ๋์ ๋ค. X
// [๋ชจ๋ฌ์ฐฝ] ๋์์...
// alert( Boolean( zipWin ) ); // false, X -> true
// ์ฐฝ์ด undefined ์ผ ๊ฒฝ์ฐ์๋ closed ์์ฑ X
// zipWin, window.closed true ์ฐฝ ๋ซํ , false ์ฐฝ ์ด๋ฆผ
// if( !zipWin ){ // false ๋ผ๋ฉด
if( !zipWin || zipWin.closed ){ // false ๋ผ๋ฉด
zipWin = window.open("ex04_02.html", "", "width=600,height=500,top=100,left=200");
// ์ด๋ ค์ ธ ์๋ ์ฐํธ๋ฒํธ๊ฒ์์ฐฝ์ด ์๋ ๋ฐ๋ ๋ ์ ์ฐฝ์ด ์ด๋ฆฐ๋ค.
}else{
zipWin.focus();
}
// X ๋ซ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋๊น.. ๋ค์๋ถํฐ๋ ๊ฒ์์ฐฝ์ด ๋์ด์ง์ง ์๋ค์.. + 5๋ถ
}
-> ์ฐํธ๋ฒํธ ๊ฒ์ ๋๋ฅด๋ฉด 04_02 ๋ธ
<table border="1" style="width:100%">
<tr>
<td>
๊ฒ์ํ ๋ ์
๋ ฅ :
<input type="text" id="dong" name="dong" />
<input type="button" value="๊ฒ์" onclick="search_dong();"/>
</td>
</tr>
<tr>
<td>
<div id="searchResult" class="box" style="visibility: hidden;">
<a href="javascript:sendAddr('123','451','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 1๋')">123-451</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 1๋<br>
<a href="javascript:sendAddr('123','452','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 2๋')">123-452</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 2๋<br>
<a href="javascript:sendAddr('123','453','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 3๋')">123-453</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 3๋<br>
<a href="javascript:sendAddr('123','454','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 4๋')">123-454</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 4๋<br>
<a href="javascript:sendAddr('123','455','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 5๋')">123-455</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 5๋<br>
<a href="javascript:sendAddr('123','456','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 6๋')">123-456</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 6๋<br>
<a href="javascript:sendAddr('123','457','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 7๋')">123-457</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 7๋<br>
<a href="javascript:sendAddr('123','458','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 8๋')">123-458</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 8๋<br>
<a href="javascript:sendAddr('123','459','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 9๋')">123-459</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 9๋<br>
<a href="javascript:sendAddr('123','461','์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 10๋')">123-461</a>์์ธ ์๋ฑํฌ๊ตฌ ๋น์ฐ 10๋<br>
</div>
</td>
</tr>
<tr>
<td>
<div class="box" style="visibility: hidden;">
'๋น์ฐ' ๊ฒ์ ๊ฒฐ๊ณผ 10๊ฐ ๊ฒ์๋์์ต๋๋ค.
</div>
</td>
</tr>
</table>
function window_unload() {
// opener.zipWin = null; // 0 , false, undefined
}
function sendAddr( zip1, zip2, addr1 ){
// self
opener.document.getElementById("zip1").value = zip1;
opener.document.getElementById("zip2").value = zip2;
opener.document.getElementById("addr1").value = addr1;
self.close();
opener.document.getElementById("addr2").focus();
}
function search_dong(){
document.getElementById("searchResult").style.visibility = "visible";
}
๊ฒ์ํด์ ์ป์ ๊ฐ์ 04์ ํํํ๊ธฐ ์ํ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋
opener : ํ์ฌ์ฐฝ์ ์ฐ ๋ถ๋ชจ์ฐฝ
๋ณดํต ์ ์ฐฝ ์๋์ฐ๊ณ ๋ชจ๋ฌ์ฐฝ ๋์!!!
* ๋์์ง ์ฐฝ ๋ซ์๋ ๋ค์ opener.zipWin = null๊ฐ ์ค์ผํจ (window_unload())
* ์๋๋ฉด zipWin.closed()
[๋ค์ ์ฐํธ๋ฒํธ OPEN API ์ฌ์ฉ] : https://postcode.map.daum.net/guide#usage
<input type="text" id="sample4_postcode" placeholder="์ฐํธ๋ฒํธ">
<input type="button" onclick="sample4_execDaumPostcode()" value="์ฐํธ๋ฒํธ ์ฐพ๊ธฐ"><br>
<input type="text" id="sample4_roadAddress" placeholder="๋๋ก๋ช
์ฃผ์">
<input type="text" id="sample4_jibunAddress" placeholder="์ง๋ฒ์ฃผ์">
<span id="guide" style="color:#999;display:none"></span>
<input type="text" id="sample4_detailAddress" placeholder="์์ธ์ฃผ์">
<input type="text" id="sample4_extraAddress" placeholder="์ฐธ๊ณ ํญ๋ชฉ">
//๋ณธ ์์ ์์๋ ๋๋ก๋ช
์ฃผ์ ํ๊ธฐ ๋ฐฉ์์ ๋ํ ๋ฒ๋ น์ ๋ฐ๋ผ, ๋ด๋ ค์ค๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํฉํ์ฌ ์ฌ๋ฐ๋ฅธ ์ฃผ์๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช
ํฉ๋๋ค.
function sample4_execDaumPostcode() {
new daum.Postcode({
oncomplete: function(data) {
// ํ์
์์ ๊ฒ์๊ฒฐ๊ณผ ํญ๋ชฉ์ ํด๋ฆญํ์๋ ์คํํ ์ฝ๋๋ฅผ ์์ฑํ๋ ๋ถ๋ถ.
// ๋๋ก๋ช
์ฃผ์์ ๋
ธ์ถ ๊ท์น์ ๋ฐ๋ผ ์ฃผ์๋ฅผ ํ์ํ๋ค.
// ๋ด๋ ค์ค๋ ๋ณ์๊ฐ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ์ ๊ณต๋ฐฑ('')๊ฐ์ ๊ฐ์ง๋ฏ๋ก, ์ด๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ถ๊ธฐ ํ๋ค.
var roadAddr = data.roadAddress; // ๋๋ก๋ช
์ฃผ์ ๋ณ์
var extraRoadAddr = ''; // ์ฐธ๊ณ ํญ๋ชฉ ๋ณ์
// ๋ฒ์ ๋๋ช
์ด ์์ ๊ฒฝ์ฐ ์ถ๊ฐํ๋ค. (๋ฒ์ ๋ฆฌ๋ ์ ์ธ)
// ๋ฒ์ ๋์ ๊ฒฝ์ฐ ๋ง์ง๋ง ๋ฌธ์๊ฐ "๋/๋ก/๊ฐ"๋ก ๋๋๋ค.
if(data.bname !== '' && /[๋|๋ก|๊ฐ]$/g.test(data.bname)){
extraRoadAddr += data.bname;
}
// ๊ฑด๋ฌผ๋ช
์ด ์๊ณ , ๊ณต๋์ฃผํ์ผ ๊ฒฝ์ฐ ์ถ๊ฐํ๋ค.
if(data.buildingName !== '' && data.apartment === 'Y'){
extraRoadAddr += (extraRoadAddr !== '' ? ', ' + data.buildingName : data.buildingName);
}
// ํ์ํ ์ฐธ๊ณ ํญ๋ชฉ์ด ์์ ๊ฒฝ์ฐ, ๊ดํธ๊น์ง ์ถ๊ฐํ ์ต์ข
๋ฌธ์์ด์ ๋ง๋ ๋ค.
if(extraRoadAddr !== ''){
extraRoadAddr = ' (' + extraRoadAddr + ')';
}
// ์ฐํธ๋ฒํธ์ ์ฃผ์ ์ ๋ณด๋ฅผ ํด๋น ํ๋์ ๋ฃ๋๋ค.
document.getElementById('sample4_postcode').value = data.zonecode;
document.getElementById("sample4_roadAddress").value = roadAddr;
document.getElementById("sample4_jibunAddress").value = data.jibunAddress;
// ์ฐธ๊ณ ํญ๋ชฉ ๋ฌธ์์ด์ด ์์ ๊ฒฝ์ฐ ํด๋น ํ๋์ ๋ฃ๋๋ค.
if(roadAddr !== ''){
document.getElementById("sample4_extraAddress").value = extraRoadAddr;
} else {
document.getElementById("sample4_extraAddress").value = '';
}
var guideTextBox = document.getElementById("guide");
// ์ฌ์ฉ์๊ฐ '์ ํ ์ํจ'์ ํด๋ฆญํ ๊ฒฝ์ฐ, ์์ ์ฃผ์๋ผ๋ ํ์๋ฅผ ํด์ค๋ค.
if(data.autoRoadAddress) {
var expRoadAddr = data.autoRoadAddress + extraRoadAddr;
guideTextBox.innerHTML = '(์์ ๋๋ก๋ช
์ฃผ์ : ' + expRoadAddr + ')';
guideTextBox.style.display = 'block';
} else if(data.autoJibunAddress) {
var expJibunAddr = data.autoJibunAddress;
guideTextBox.innerHTML = '(์์ ์ง๋ฒ ์ฃผ์ : ' + expJibunAddr + ')';
guideTextBox.style.display = 'block';
} else {
guideTextBox.innerHTML = '';
guideTextBox.style.display = 'none';
}
}
}).open();
}
BOM screen
[window.screen ๊ฐ์ฒด]
- ์ฌ์ฉ์ ํ๋ฉด์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด
screen.width ํ๋ฉด ๋๋น
screen.height ํ๋ฉด ๋์ด
screen.availWidth: ์ฌ์ฉ๊ฐ๋ฅํ ํ๋ฉด ๋๋น
screen.availHeight: ์ฌ์ฉ๊ฐ๋ฅํ ํ๋ฉด ๋์ด
scren.colorDepth ์ ๋๋
16๋นํธ : 65536๊ฐ์ง
32๋นํธ : 42์ต๊ฐ์ง
๋ฑ๋ฑ
<p id="demo"></p>
var output = "";
output += screen.width +" / " + screen.height + "<br>";
output += screen.availWidth +" / " + screen.availHeight + "<br>";
output += screen.colorDepth +"<br>";
$("#demo").html( output );
BOM location
1. window
2. screen
3. window.location ๊ฐ์ฒด
- ์๋์ฐ(์ฐฝ) ์์น
- ํ์ฌ ํ์ด์ง ์ฃผ์(URL)๋ฅผ ๊ฐ์ ธ์ค๊ณ
- ํ์ฌ ํ์ด์ง ์ฃผ์(URL)๋ฅผ ์๋ก ์ค์ -> ์ด๋ ( redirect )
<a href="ex01.html?msg=hello&age=10">ex01.html</a>
<br>
<button onclick="move()">URL์ด๋</button>
<br>
<br>
<br>
<button onclick="history.back()">์ด์ ํ์ด์ง๋ก ์ด๋</button>
var url = location.href;
console.log( url )
console.log( location.hostname ) ; // ๋๋ฉ์ธ์ด๋ฆ ๋ฐํ
console.log( location.protocol ) ; // ์น ํ๋กํ ์ฝ ๋ฐํ
console.log( location.port ) ; // ํฌํธ๋ฒํธ ๋ฐํ
function move(){
// ์์ฉ๊ต์ก์ผํฐ ์ฌ์ดํธ
// location.href = "http://www.sist.co.kr"; ์์ฑ *****
// console.log( location.href ); ํ์ฌ ํ์ด์ง ์ฃผ์(URL)๋ฅผ ๊ฐ์ ธ์ค๊ณ
location.assign("http://www.sist.co.kr"); // ์ ํ์ด์ง๋ก ์ด๋ , ํจ์
}
aํ๊ทธ ์ด๋
<a href="ex06.html">ex06.html</a>
$("a").click(function() {
// console.log("xxx");
var msg = "hello world";
var age = 20;
location.href="http://www.sist.co.kr?msg=" + msg +"&age=" + age;
});
1. window
2. screen
3. location
4. window.history
- ๋ธ๋ผ์ฐ์ ๊ธฐ๋ก์ด ํฌํจํ๊ณ ์๋ ๊ฐ์ฒด
- ์ฌ์ฉ์์ ๊ฐ์ธ ์ ๋ณด๋ฅผ ๋ณดํธํ๊ธฐ ์ฐํด ์ ๊ทผ ๋ฐฉ๋ฒ์ด ์ ํ์ ์ด๋ค.
5. history.back() ๋ธ๋ผ์ฐ์ ์์ ๋ค๋ก ๊ฐ๊ธฐ ๋ฒํผ == history.go(-1)
history.forward() ๋ธ๋ผ์ฐ์ ์์ ์์ผ๋ก๋ก ๊ฐ๊ธฐ ๋ฒํผ == history.go(1)
[js BOM]
1. window
2. screen
3. location
4. history
5. navigator
- ๋ฐฉ๋ฌธ์ ๋ธ๋ผ์ฐ์ ์ ๋ณด๋ฅผ ๊ฐ๊ณ ์๋ ๊ฐ์ฒด ..
A ์ฌ๋ - ํฌ๋กฌ 10.XX -> ์์ฒญ
console.log( navigator.appName +"<br>"); // ๋ธ๋ผ์ฐ์ ์์ฉ ํ๋ก๊ทธ๋จ ์ด๋ฆ
console.log( navigator.appVersion +"<br>");
console.log( navigator.product +"<br>"); // ๋ธ๋ผ์ฐ์ ์ ์์ง ์ด๋ฆ
console.log( navigator.cookieEnabled +"<br>"); // ๋ธ๋ผ์ฐ์ ์ ์ฟ ํค ์ฌ์ฉ ์ฌ๋ถ ( true )
console.log( navigator.javaEnabled +"<br>"); // java ํ์ฑํ ์ฌ๋ถ
console.log( navigator.platform +"<br>"); // ๋ธ๋ผ์ฐ์ ํ๋ซํผ(์ด์์ฒด์ )
[ js ํ์ ์์ ]
1. window.alert("๋ฌธ์์ด")
2. confirm() ํ์ธ/ ์ทจ์ ๋ฒํผ ๊ณผ ๋ฉ์์ง ์ถ๋ ฅ
3. prompt()
// alert( "๊ฒฝ๊ณ ์ฐฝ - ๋ํ์์");
var message = "์ ๋ง ์ข
๋ฃํ ๊น์? ";
var result = confirm(message); // ํ์ธ(true), ์ทจ์(false)
if( result ){ // ํ์ธ ๋ฒํผ ํด๋ฆญ...
self.close();
}
var age = prompt("๋์ด๋ฅผ ์
๋ ฅํ์ธ์ ? ", 20);
alert( age ); // ํ์ธ( ์
๋ ฅ๊ฐ ), ์ทจ์( null )
js HTML DOM
js ์ฟ ํค
<style>
.box{
position: absolute;
}
</style>
<img alt="" src="../images/item01.gif">
function getRandom( min, max){
return Math.floor( Math.random()*(max-min+1)+min);
}
// ์ค๋ฅธ์ชฝ ๋ง์ฐ์ค๋ฅผ ํด๋ฆญํ ๋ ๋์์ง๋ ๋ฉ๋ด : ์ปจํ
์คํธ ๋ฉ๋ด
document.oncontextmenu = function (){
// alert("์ค๋ฅธ์ชฝ ๋ง์ฐ์ค๋ ์ฌ์ฉํ ์ ์์ต๋๋ค. ");
return false; // ์ด๋ฒคํธ ์ทจ์
}
// item01.gif~ item21.gif
document.onmousedown = function (){
// ์ผ์ชฝ๋ง์ฐ์ค (0)
// ์ค๋ฅธ์ชฝ ๋ง์ฐ์ค( 2)
// ๊ฐ์ด๋ฐ ํ ๋ง์ฐ์ค( 1)
// console.log( event.button );
if( event.button == 0 ){ // ์ผ์ชฝ ๋ง์ฐ์ค๋ฅผ ํด๋ฆญ...ํ ๋.
// jquery method : width(), height()
document.title = event.clientX +", " + event.clientY +" / " + $("img").width();
// <img>
var img = document.createElement("img");
// src ์์ฑ, alt ์์ฑ ํ์
var imgNo = getRandom(1, 21).toString().padStart(2, 0);
var imgUrl = "../images/item"+ imgNo+".gif"
img.setAttribute("src", imgUrl);
img.classList.add( "box");
var left = event.clientX - $("img").width()/2;
var top = event.clientY - $("img").height()/2;
img.setAttribute("style", "left:"+left+"px;top:"+top+"px");
document.body.appendChild(img);
} else if( event.button == 2 ){ // ์ค๋ฅธ์ชฝ ๋ง์ฐ์ค ํด๋ฆญ..
document.body.removeChild( event.srcElement );
} else if( event.button == 1 ){ // ๊ฐ์ด๋ฐ ํ ๋ง์ฐ์ค( 1)
var imgs = document.querySelectorAll("img.box");
for (let img of imgs) {
// document.body.removeChild(img);
img.remove();
}
}// if
}
* ์ซ์ 0004 ํํ๋ฒ๋ ์ ์ฝ๋์์ ์์
๋ชจ๋ฌ์ฐฝ ***
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
label, input { display:block; }
input.text { margin-bottom:12px; width:95%; padding: .4em; }
fieldset { padding:0; border:0; margin-top:25px; }
h1 { font-size: 1.2em; margin: .6em 0; }
div#users-contain { width: 350px; margin: 20px 0; }
div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
.ui-dialog .ui-state-error { padding: .3em; }
.validateTips { border: 1px solid transparent; padding: 0.3em; }
<div id="dialog-form" title="Create new user">
<p class="validateTips">All form fields are required.</p>
<form>
<fieldset>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="Jane Smith" class="text ui-widget-content ui-corner-all">
<label for="email">Email</label>
<input type="text" name="email" id="email" value="jane@smith.com" class="text ui-widget-content ui-corner-all">
<label for="password">Password</label>
<input type="password" name="password" id="password" value="xxxxxxx" class="text ui-widget-content ui-corner-all">
<!-- Allow form submission with keyboard without duplicating the dialog button -->
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>
<div id="users-contain" class="ui-widget">
<h1>Existing Users:</h1>
<table id="users" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header ">
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>john.doe@example.com</td>
<td>johndoe1</td>
</tr>
</tbody>
</table>
</div>
<button id="create-user">Create new user</button>
// $( function (){ });
$( function() {
// ์ ์ญ ๋ณ์
var dialog, form,
// From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
name = $( "#name" ),
email = $( "#email" ),
password = $( "#password" ),
allFields = $( [] ).add( name ).add( email ).add( password ),
tips = $( ".validateTips" );
function updateTips( t ) {
tips
.text( t )
.addClass( "ui-state-highlight" );
setTimeout(function() {
tips.removeClass( "ui-state-highlight", 1500 );
}, 500 );
}
function checkLength( o, n, min, max ) {
if ( o.val().length > max || o.val().length < min ) {
o.addClass( "ui-state-error" );
updateTips( "Length of " + n + " must be between " +
min + " and " + max + "." );
return false;
} else {
return true;
}
}
function checkRegexp( o, regexp, n ) {
if ( !( regexp.test( o.val() ) ) ) {
o.addClass( "ui-state-error" );
updateTips( n );
return false;
} else {
return true;
}
}
function addUser() {
var valid = true; // ์ ํจ์ฑ ๊ฒ์ฌ ๊ฒฐ๊ณผ...
//
allFields.removeClass( "ui-state-error" );
valid = valid && checkLength( name, "username", 3, 16 );
valid = valid && checkLength( email, "email", 6, 80 );
valid = valid && checkLength( password, "password", 5, 16 );
valid = valid && checkRegexp( name, /^[a-z]([0-9a-z_\s])+$/i, "Username may consist of a-z, 0-9, underscores, spaces and must begin with a letter." );
valid = valid && checkRegexp( email, emailRegex, "eg. ui@jquery.com" );
valid = valid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( valid ) {
$( "#users tbody" ).append( "<tr>" +
"<td>" + name.val() + "</td>" +
"<td>" + email.val() + "</td>" +
"<td>" + password.val() + "</td>" +
"</tr>" );
dialog.dialog( "close" );
// DB insert + jquery ajax
}
return valid;
}
// /jquery-ui.js"
dialog = $( "#dialog-form" ).dialog({
autoOpen: false,
height: 400,
width: 350,
modal: true,
buttons: {
"๊ณ์ ์ถ๊ฐ": addUser,
Cancel: function() {
dialog.dialog( "close" );
}
},
close: function() {
form[ 0 ].reset();
allFields.removeClass( "ui-state-error" );
}
});
form = dialog.find( "form" ).on( "submit", function( event ) {
event.preventDefault();
addUser();
});
$( "#create-user" ).button().on( "click", function() {
dialog.dialog( "open" );
});
} );
์ต๊ทผ๋๊ธ