[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" );
    }); 
  } );
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ