[Day8] JS 8 [11/29] 

 

๋ชจ๋‹ฌ/๋ชจ๋‹ฌ๋ฆฌ์Šค์ฐฝ

 .modal{
    display:none;    
  
    position: fixed;
    z-index: 1;
    padding-top: 100px;
    left:0;
    top:0;
	    width:100%;   
	    height:100%;
    overflow: auto;
    background-color: rgb(0,0,0);
    background-color:rgba(0,0,0,0.4);
  }
  
   .modal-content{
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding:0;
    border:1px solid #888;
    width: 80%;
    box-shadow: 0 4px 9px 0  rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
    
    animation-name:animatetop;
    animation-duration: 0.4s;
    
    -webkit-animation-name:animatetop;
    -webkit-animation-duration: 0.4s;
  }
  
   
  /* ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ */
  @keyframes animatetop{
     from{ top:-300px; opacity: 0; }
     to{ top:0; opacity: 1; }
  }
  @-webkit-keyframes animatetop{
     from{ top:-300px; opacity: 0; }
     to{ top:0; opacity: 1; }
  }

  .modal-header, .modal-footer{
    background-color: #5CB85C;
    padding: 2px 16px;
    color:white;
  }
  
  .modal-body{
    padding: 2px 16px;
  }
  
 .close{
    color:white;
    float:right;
    font-size: 28px;
    font-weight: bold;
  }
  
  .close:hover, .close:focus{
    color:#000;
    text-decoration: none;
    cursor: pointer;
  }
<button id="myBtn">Open Modal</button>

<div id="myModal" class="modal">
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">&times;</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Lorem ipsum dolor sit.</p>
      <p>Eum libero ipsum aspernatur.</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>    
  </div>
</div>

DOM

   1. DOM ?

      - Document Object Model์˜ ์•ฝ์ž

      - ์›น ํŽ˜์ด์ง€ -> ์š”์ฒญ(request) ->  ์›น์„œ๋ฒ„ 

                        <- ์‘๋‹ต(response)

         ์‘๋‹ต๋œ ์›นํŽ˜์ด์ง€๊ฐ€ ๋กœ๋”ฉ ๋˜๋ฉด   ๋ธŒ๋ผ์šฐ์ €๋Š”  ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(DOM)์„ ์ƒ์„ฑํ•œ๋‹ค.     

          html ์›นํŽ˜์ด์ง€ -> [html DOM]

          xml    ์›นํŽ˜์ด์ง€ -> xml DOM

   1-2. DOM ์ƒ์„ฑ๋œ  ํŠธ๋ฆฌ ๊ตฌ์กฐ     

          [Document]  Node(๋…ธ๋“œ)  

                    |

             [html ํƒœ๊ทธ] Root element , Node(๋…ธ๋“œ)    

 

  [head ํƒœ๊ทธ]      [bodyํƒœ๊ทธ]

          |                           |

    [title ํƒœ๊ทธ]       [a ํƒœ๊ทธ ]   []href ์†์„ฑ]

      |                                       

     [Text SS17 2022. 11. 29. ์˜ค์ „ 5:01:03] Node 

          

          Node : element, attribute, text, comment ๋“ฑ๋“ฑ

          

   2. DOM ์™œ ์ƒ์„ฑ(์‚ฌ์šฉ)

      -    js + html ์š”์†Œ ๋ณ€๊ฒฝ             , ์ถ”๊ฐ€, ์‚ญ์ œ

      -    js + html ์†์„ฑ ๋ณ€๊ฒฝ             , ์ถ”๊ฐ€, ์‚ญ์ œ

      -    js + css    ์Šคํƒ€์ผ ๋ณ€๊ฒฝ, ์ถ”๊ฐ€, ์‚ญ์ œ

      - ๋“ฑ๋“ฑ  

      

    3. W3C DOM  ์ข…๋ฅ˜

               - Core DOM - ๋ชจ๋“  ๋ฌธ์„œ ์œ ํ˜•์— ๋Œ€ํ•œ ํ‘œ์ค€ DOM 

               - html DOM

               - xml   DOM

               

    4. DOM ๋ฉ”์„œ๋“œ 

        ใ„ฑ. html ์š”์†Œ ์ฐพ๋Š” ๋ฉ”์„œ๋“œ 

          document.getElementById()        

          document.getElementsByTagName( tagname  ) 

      document.getElementsByClassName( className)

          document.getElementsByName( name )  

          document.querySelector()

          document.querySelectorAll()

        ใ„ด. html ์š”์†Œ ๋ณ€๊ฒฝ

            element.innerHTML

            element.innerText

            element.์†์„ฑ๋ช…

            element.style.์†์„ฑ๋ช…

            element.setAttribute()

            element.getAttribute()

         ใ„ท. html ์š”์†Œ ์ƒ์„ฑ, ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œ

            document.createElement()

            document.appendChild()

            document.removeChild()

            document.reomve()

            document.replace( new, old ) ***

            document.write("<font></font>");

          ใ„น. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ.

          element.on์ด๋ฒคํŠธ๋ช… = function (){}

          element.onmousemove = function (){}       

          :

          :

          

        ใ„น. 1998   html DOM LEVEL 1 ( 11๊ฐœ์˜ html ๊ฐ์ฒด, ์ปฌ๋ ‰์…˜ , ์†์„ฑ ์ •์˜)

              html DOM Level 3 ( ๋” ๋งŽ์€ ๊ฐ์ฒด, ์ปฌ๋ ‰์…˜, ์†์„ฑ ์ถ”๊ฐ€)

              html 5  ์œ ํšจ..

              

         ใ…. html ๊ฐ์ฒด 

             document.links ์ปฌ๋ ‰์…˜

             document.images ์ปฌ๋ ‰์…˜

             document.forms

             :

             document.scripts ์ปฌ๋ ‰์…˜ == ๋ชจ๋“  script ์š”์†Œ

             

             document.body  == <body>์š”์†Œ

             document.documentElement == <html> ์š”์†Œ

             document.title == <title>์š”์†Œ

             

             document.doctype = <!DOCTYPE> ์š”์†Œ

             *** document.cookie   == ์ฟ ํ‚ค ๊ฐ์ฒด ***

             document.head == <head>์š”์†Œ

             ๋“ฑ๋“ฑ

 

DOM ํƒ์ƒ‰

   1. html ๋ฌธ์„œ์˜ ๋ชจ๋“  ๊ฒƒ์€ Node(๋…ธ๋“œ)์ด๋‹ค.

   2. Document ๋…ธ๋“œ  == ์ „์ฒด ๋ฌธ์„œ

      

       <span style="">text</span> 

       ์š”์†Œ  ๋…ธ๋“œ (element node)

       ์†์„ฑ  ๋…ธ๋“œ ( attribute node)

       ํ…์ŠคํŠธ ๋…ธ๋“œ( Text node)

       ์ฃผ์„ ๋…ธ๋“œ( comment node)

       

    3. ๋ชจ๋“  ๋…ธ๋“œ๋“ค์€ ๊ด€๊ณ„ ๋งบ๊ณ  ์žˆ๋‹ค. 

        - ๋ถ€๋ชจ : ์ž์‹

           parentNode

           childNode

           

        - ํ˜•์ œ

        next[Element]Sibling   ๋‹ค์Œ ํ˜•์ œ ์š”์†Œ         

        [A E NODE]  - [B A NODE] - [C E NODE]

             (๊ธฐ์ค€)                  X

        nextSibling  ๋‹ค์Œ ํ˜•์ œ ๋…ธ๋“œ    

        

        previousElementSibling  ์ด์ „ ํ˜•์ œ ์š”์†Œ

        previousSibling                ์ด์ „ ํ˜•์ œ ๋…ธ๋“œ

        

        - ์ž์‹ ๋…ธ๋“œ 

           firstChild ์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ

           lastChild   ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ

           

     4. ๋…ธ๋“œ ํŠธ๋ฆฌ์—์„œ ์ตœ์ƒ์œ„ ๋…ธ๋“œ ?     (  html ๋…ธ๋“œ   == ๋ฃจํŠธ ๋…ธ๋“œ )

         ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋…ธ๋“œ๋“ค์€ ํ•˜๋‚˜์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง„๋‹ค. 

         ํ•˜๋‚˜์˜ ๋…ธ๋“œ๋Š” ์—ฌ๋Ÿฌ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. 

         ํ˜•์ œ ๋…ธ๋“œ๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๊ฐ™์€ ๋…ธ๋“œ๋“ค์ด๋‹ค. 

           

       5. ๋…ธ๋“œ ๋“ค ๊ฐ„์˜ ํƒ์ƒ‰ํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ

           1) parentNode ์†์„ฑ : ๋ถ€๋ชจ๋…ธ๋“œ

           2) childNodes ์†์„ฑ : ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ ๋ชฉ๋ก  nodeList

           3) firstChild ์†์„ฑ

           4) lastChild ์†์„ฑ

           5) nextSibling ์†์„ฑ

           6) previousSibling ์†์„ฑ

 

    [๊ฐ ๋…ธ๋“œ ์†์„ฑ]

    1. nodeValue ์†์„ฑ

        1) text node :  ๋ฌธ์ž์—ด ์ž์ฒด

        2) element node :    null

        3) attribute node :  ์†์„ฑ๊ฐ’

        

    2. nodeName ์†์„ฑ

        1) text node : #text

        2)element node : H1

        3) attribute node :  ์†์„ฑ๋ช…

        4) comment node : #comment

        

    3. nodeType ์†์„ฑ

        1) text node : 3

        2) element node : 1 

        3) attribute node :  2

        4) comment node : 4

        5) ๋ฃจํŠธ ๋…ธ๋“œ == html ๋…ธ๋“œ : 5

        6) ์„ ์–ธ๋ฌธ ๋…ธ๋“œ : 6

<p id="demo" style="border:1px solid gray"></p>
document.getElementById("demo").innerHTML = 
// document.body.innerHTML;           // body ์š”์†Œ
document.documentElement.innerHTML;   // html ์š”์†Œ

document.getElementById("id02").innerHTML =
// element node, [text node], attribute node, comment node
// nodeValue ์†์„ฑ ? 
// document.getElementById("id01").firstChild.nodeValue;
// document.getElementById("id01").firstChild.nodeType;
// document.getElementById("id01").firstChild.nodeName;
	   
// document.getElementById("id01").nodeValue;
// document.getElementById("id01").nodeType;
// document.getElementById("id01").nodeName;
	   
// document.getElementById("id01").childNodes.length;  // 1
	   
document.getElementById("id01").childNodes[0].nodeValue;

 

ex)

<!-- #div1>p#id$*2>lorem4 -->
<div id="div1">
    <p id="id1">Lorem ipsum dolor sit.</p>
	<p id="id2">Ad necessitatibus perspiciatis deleniti.</p>
</div>
  var parentNode = document.getElementById("div1");
  // alert(  parentNode.nodeName   );  //DIV  element node
  
  //  alert(  parentNode.firstChild.nodeName );  // #text
  //alert(  parentNode.firstChild.nodeValue);  // #text
  
  //  alert(   parentNode.firstElementChild.nodeName );
  
////////////////////////////////////////////////////////////////

   // 1.  DOM method ์‚ฌ์šฉํ•ด์„œ  ์ƒˆ๋กœ์šด ์š”์†Œ ์ƒ์„ฑ ์ถ”๊ฐ€
   // <p id="id3">hong gil dong.</p>
   var p3 = document.createElement("p");
   // p3.id = "id3";
   p3.setAttribute("id", "id3");
   
   // p3.innerHTML = "hong gil dong.";   
   // 1-2. text node ์ƒ์„ฑ
   var content = "hong gil dong.";
   var textNode = document.createTextNode(content);
   p3.appendChild(textNode)
   
   // ใ„ฑ.  appendChild()  ๋ถ€๋ชจ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹์œผ๋กœ ์ถ”๊ฐ€.. ๋ฉ”์„œ๋“œ  
   var parentNode = document.getElementById("div1");
   //parentNode.appendChild(p3);
   
   // ใ„ด. ๋ถ€๋ชจ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹์œผ๋กœ ์ถ”๊ฐ€...  insertBefore( newelemt, before)
   // ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์š”์†Œ ์•ž์— ์ถ”๊ฐ€ ํ•  ๋•Œ ์‚ฌ์šฉ. 
   var p1 = parentNode.firstChild;
   parentNode.insertBefore( p3 , p1);
   
   // ใ„น.  p1 ์ œ๊ฑฐ
   var p1 = document.getElementById("id1");
   // p1.remove();  // ์ž๊ธฐ์ž์‹ ๋…ธ๋“œ ์ œ๊ฑฐ
   parentNode.removeChild(p1);  // ์ž์‹ ๋…ธ๋“œ ์ œ๊ฑฐ 

   // ใ„ท. p1 = p3 ๊ต์ฒด
   parentNode.replaceChild(p3, p1);

 

ex2)

์ „์ฒด ์„ ํƒ : <input type="checkbox" />
<table border="1"> 
  <tr>     
     <td><input type="checkbox" />1</td>
     <td>hong</td>
     <td>seoul</td>
  </tr>
  <tr>
     <td><input type="checkbox" />2</td>
     <td>admin</td>
     <td>seoul</td>
  </tr>
  <tr>
     <td><input type="checkbox"  />4</td>
     <td>kenik</td>
     <td>pohang</td>
  </tr>
</table> 
 
<button id="btn">๋ฒ„ํŠผ</button>

<p id="demo">
 1 / hong / seoul
</p>
document.getElementById("btn").onclick = function(){
	// ์ฒดํฌ๋œ ์ฒดํฌ๋ฐ•์Šค 
	var ckbs = document.querySelectorAll("table input[type=checkbox]");
	for (let ckb of ckbs) {
		if( ckb.checked ){
			// DOM ํƒ์ƒ‰           ckb ์š”์†Œ๋…ธ๋“œ ์˜ ๋’ค ํ˜•์ œ ๋…ธ๋“œ 1 : ํ…์ŠคํŠธ ๋…ธ๋“œ
			//  ckb.nextElementSibling  X
		    // alert( 	ckb.nextSibling.nodeName +" / " + ckb.nextSibling.nodeValue );
		    var no = ckb.nextSibling.nodeValue ;
		    // alert( ckb.parentNode.nextElementSibling.innerHTML ) ;  // TD
		    // alert(  ckb.parentNode.nextElementSibling.firstChild.nodeName );
		    var name = ckb.parentNode.nextElementSibling.firstChild.nodeValue;		    
		    var loc = ckb.parentNode.nextElementSibling.nextElementSibling.firstChild.nodeValue;
		    
		    console.log( no + "/ " + name + " / " + loc  );
		    
		} // if
	} // for
}

 

๊ณต์ง€์‚ฌํ•ญ

  *{
      box-sizing: border-box;
   }
    .alert{
      padding: 20px;
      background-color: #f44336;
      color:white;
      opacity: 1; 
      transition:opacity 0.6s, height 0.6s;
      margin-bottom: 15px;
   }
   
    .closebtn{
     margin-left: 15px;
     color:white;
     font-weight: bold;
     float:right;
     font-size: 22px;
     line-height: 20px;
     cursor:pointer;
     transition:0.3s;
   }
   .closebtn:hover{
      color:black;
   }
   
   .alert.success{     background-color: #4caf50;   }
   .alert.info{      background-color: #2196f3;   }
   .alert.warning{     background-color: #ff9800;   }
<h2>์•Œ๋ฆผ(๊ณต์ง€์‚ฌํ•ญ)</h2>
๊ณต์ง€์‚ฌํ•ญ ํƒ€์ž… : 
<select name="type" id="type">
   <option>type ์—†์Œ</option>
   <option>success</option>
   <option>warning</option>
   <option>info</option>
</select>
<br>
๋‚ด์šฉ  : <input type="text" />
<button>๊ณต์ง€์‚ฌํ•ญ ์ถ”๊ฐ€</button>
<hr>
    window.onload = function (){
    	// [] ๋ฐฐ์—ด
    	//  {} ๊ฐ์ฒด
    	var data =
    		     [
    		             {type:"success", message:"Lorem ipsum dolor sit amet, consectetur adipisicing."},
    		             {type:"warning", message:"Lorem ipsum dolor sit amet, consectetur adipisicing."},
    		             {type:"info", message:"Lorem ipsum dolor sit amet, consectetur adipisicing."},
    		             {type:"success", message:"Lorem ipsum dolor sit amet, consectetur adipisicing."}
    	         ];
    	/* DOM ์‚ฌ์šฉํ•ด์„œ ์š”์†Œ ์ƒ์„ฑ ์ถ”๊ฐ€. 
    	<div class="alert info">
    	  <span class="closebtn">&times;</span>
    	  <strong>Info!</strong>
    	  Lorem ipsum dolor sit amet, consectetur adipisicing.
    	</div> 
    	 */
    	 for (var i = 0; i < data.length; i++) {
    		 
    		 var type =  data[i].type;
    		 var message = data[i].message;
    		 
    		 // <div class="alert info"></div>
    		 var alertDiv = document.createElement("div");
    		 alertDiv.classList.add( "alert"  );
    		 alertDiv.classList.add( type  );
    		 
    		 // <span class="closebtn">&times;</span>
    		 var spanbtn = document.createElement("span");
    		 spanbtn.className = "closebtn";
    		 //  document.createTextNode(content)
    		 spanbtn.innerHTML = "&times;";
    		 spanbtn.onclick = function (){
    			 // x  ํ•ด๋‹น ์•Œ๋ฆผ์ฐฝ ๋‹ซ๊ธฐ.
    			 // this.parentNode;
    			 // DOM ํƒ์ƒ‰~
    			 var div =  this.parentElement;
    			 // div.remove();
    			 div.style.opacity = "0";
    			 setTimeout( function (){
    				 div.style.display = "none";
    			 }, 600);
    		 } // onclick
    		 alertDiv.appendChild(spanbtn);
    		 
    		 
    		// <strong>Info!</strong>
    		 var strong = document.createElement("strong");  
    		 strong.innerHTML =  type.toUpperCase();
    		 alertDiv.appendChild(strong);
    		 
    		 // alertDiv.innerHTML = message;
    		 var content =  document.createTextNode( message );
    		 alertDiv.appendChild(content);
    		 
    		 // hr ๋’ค์— ์ถ”๊ฐ€
    		 var hr =  document.querySelector("hr");
    		 // insertBefor();
    		 document.body.insertBefore(alertDiv,   hr.nextElementSibling  )
		} // for  
    } // onload
  
    
    document.querySelector("button").onclick = function(){
    	  var type = document.querySelector("#type").value;
    	  var message = document.querySelector("input[type=text]").value;
    	  
    	  
    	// <div class="alert info"></div>
 		 var alertDiv = document.createElement("div");
 		 alertDiv.classList.add( "alert"  );
 		 alertDiv.classList.add( type  );
 		 
 		 // <span class="closebtn">&times;</span>
 		 var spanbtn = document.createElement("span");
 		 spanbtn.className = "closebtn";
 		 //  document.createTextNode(content)
 		 spanbtn.innerHTML = "&times;";
 		 spanbtn.onclick = function (){
 			// this.parentNode;
			 var div =  this.parentElement;
 			// div.remove();
			 div.style.opacity = "0";
			 setTimeout( function (){
				 
				 div.style.display = "none";
			 }, 600);
 		 } // onclick
 		 alertDiv.appendChild(spanbtn);
 		 
 		 
 		// <strong>Info!</strong>
 		 var strong = document.createElement("strong");  
 		 strong.innerHTML =  type.toUpperCase();
 		 alertDiv.appendChild(strong);
 		 
 		 // alertDiv.innerHTML = message;
 		 var content =  document.createTextNode( message );
 		 alertDiv.appendChild(content);
 		 
 		 // hr ๋’ค์— ์ถ”๊ฐ€
 		 var hr =  document.querySelector("hr");
 		 // insertBefor();
 		 document.body.insertBefore(alertDiv,   hr.nextElementSibling  )
    }

   [ js ์ฟ ํ‚ค( Cookie ) ]

   1. ๋ณธ์ธ ์ปดํ“จํ„ฐ(ํด๋ผ์ด์–ธํŠธ)์— ์ž‘์€ ํ…์ŠคํŠธํŒŒ์ผ๋กœ ์ €์žฅ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฟ ํ‚ค๋ผ๊ณ  ํ•œ๋‹ค.

   2. ํ•œ๋ผ์ธ์˜ ๋ฌธ์ž์—ด

       "์ฟ ํ‚ค๋ช…=์ฟ ํ‚ค๊ฐ’;์ฟ ํ‚ค๋ช…=์ฟ ํ‚ค๊ฐ’;name=admin;์ฟ ํ‚ค๋ช…=์ฟ ํ‚ค๊ฐ’;""    (20๊ฐœ ๊นŒ์ง€ ์ €์žฅ)

   2-2.  ๋ฌธ์ž์—ด ํŒŒ์‹ฑ...     

    3. ๋ธŒ๋ผ์šฐ์ € ์ฟ ํ‚ค ์‚ฌ์šฉ ํ—ˆ์šฉ ์—ฌ๋ถ€ ๊ฒฐ์ •

    4.  js ์ฟ ํ‚ค ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

        "document.cookie ์†์„ฑ"์„์‚ฌ์šฉํ•ด์„œ ์ฟ ํ‚ค๊ฐ’์„ ์ €์žฅ/ ์ฝ๊ธฐ.

    5. ์ฟ ํ‚ค ํ˜•์‹    

     name="nameValue"; expires="expireDate"; path="pathHolders"; domain="domainName"; secure 

     ์ฟ ํ‚ค๋ช…=์ฟ ํ‚ค๊ฐ’;_๋งŒ๋ฃŒ์‹œ์ =๋‚ ์งœ;_path=/;_๋„๋ฉ”์ธ=localhost; ๋ณด์•ˆ์ฒ˜๋ฆฌ    

       

   

   ์ƒํƒœ๊ด€๋ฆฌ - [ ์›น ํŽ˜์ด์ง€ ๋ฐ”๋€” ๋•Œ ๋งˆ๋‹ค ์ƒํƒœ (์ธ์ฆ, ๊ถŒํ•œ)  ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌ. ]   

   login.html

   ์•„์ด๋””

   ๋น„๋ฐ€๋ฒˆํ˜ธ

   [๋กœ๊ทธ์ธ ]       -> ์„œ๋ฒ„ ์ธ์ฆ   

   main.html   <-

   

   ์„œ๋ฒ„ ์ƒํƒœ ๊ด€๋ฆฌ         : ์˜ค๋ผํด, ์„ธ์…˜(Session)

   ํด๋ผ์ด์–ธํŠธ ์ƒํƒœ ๊ด€๋ฆฌ : ์ฟ ํ‚ค , hidden ํƒœ๊ทธ

   

   

   1) ๊ฒŒ์‹œํŒ

   2) ๋ณด์•ˆ์ฒ˜๋ฆฌ ( ์ธ์ฆ, ๊ถŒํ•œ )  ์ฟ ํ‚ค/์„ธ์…˜

 

-------------------

  1. ์ฟ ํ‚ค ์ƒ์„ฑ

  2. ์ฟ ํ‚ค ์‚ญ์ œ

  3. ์ฟ ํ‚ค ํ™•์ธ

  4. ์ฟ ํ‚ค ์ˆ˜์ •( ๋‹ค์‹œ ๊ฐ™์€ ์ฟ ํ‚ค๋ช…์œผ๋กœ ์ƒ์„ฑ )

  

  ์ฟ ํ‚ค ์ƒ์„ฑ 2๊ฐ€์ง€ - ํด๋ผ์ด์–ธํŠธ ( ์ปดํ“จํ„ฐ -ํ…์ŠคํŠธ ํŒŒ์ผ ์ €์žฅ)

   1) ํด๋ผ์ด์–ธํŠธ  js ์ฟ ํ‚ค ์ƒ์„ฑ, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

   2) ์„œ๋ฒ„          jsp ์ฟ ํ‚ค ์ƒ์„ฑ, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•    => ์‘๋‹ต + ์ฟ ํ‚ค์ƒ์„ฑ

   

   document.cookie ์†์„ฑ : ์ €์žฅ, ์ฝ๊ธฐ.

   

   cookie.js

function setCookie( cname, cvalue,  expiresdays) {
    	var now = new Date();   
    	now.setDate(  now.getDate() + expiresdays );
		 document.cookie =  cname +  "=" + escape(cvalue) + "; expires=" + now.toUTCString() ;
	}
	
	function getAllCookies() { 
		var cookies =  document.cookie; 
		return  cookies;
	}
	 
	function getCookie( cname ) {
		var cvalue = null;		
		var cookies =  document.cookie; 
		
		var cookieArray = cookies.split( /;\s/ );   // string or regexp
		for (var i = 0; i < cookieArray.length; i++) {
			 var cnv = cookieArray[i].split("="); 
			  if(  cname == cnv[0] ){
				  var cvalue =  cnv[1];
				  return   unescape( cvalue );
			  }
		}
		
		return cvalue;
		
	}
	function delCookie( cname ) { 	      
    	var now = new Date();   
    	now.setDate(  now.getDate() - 10 ); 
		 document.cookie =  cname +  "=; expires=" + now.toUTCString() ;  
	}
์ฟ ํ‚ค์ด๋ฆ„ :  <input type="text" id="cookieName"><br> 
์ฟ ํ‚ค๊ฐ’ :  <input type="text" id="cookieValue"><br>   <!-- ํ™๊ธธ๋™  => escape( ASCII ์ธ์ฝ”๋”ฉ ์ €์žฅ  ) -->
<br>

<button onclick="setCookie();">์ฟ ํ‚ค ์ƒ์„ฑ(์ˆ˜์ •)</button>
<button onclick="getAllCookies();">๋ชจ๋“  ์ฟ ํ‚ค ํ™•์ธ</button>
<button onclick="getCookie();">์ฟ ํ‚ค ํ™•์ธ</button>
<button onclick="delCookie();">์ฟ ํ‚ค ์‚ญ์ œ</button>

<p id="demo"></p>
    function setCookie() {
    	var cname = document.getElementById("cookieName").value;
    	var cvalue = document.getElementById("cookieValue").value;
    	
    	var now = new Date();  // ์˜ค๋Š˜๋‚ ์งœ + 10 ์ผ ์ถ”๊ฐ€
    	now.setDate(  now.getDate() + 10 );
    	// console.log( now.toLocaleString() );
    	
    	// ์ฟ ํ‚ค์ด๋ฆ„="์ฟ ํ‚ค๊ฐ’"; ๋งŒ๋ฃŒ์‹œ์ ="๋‚ ์งœ"; ๊ฒฝ๋กœ="/"; ๋„๋ฉ”์ธ="๋„๋ฉ”์ธ๋ช…"; ๋ณด์•ˆ(true/false)
		 document.cookie =  cname +  "=" + escape(cvalue) + "; expires=" + now.toUTCString() ;  //  ๋ฌธ์ž์—ด ํ˜•์‹
		 
		 document.getElementById("cookieName").value = "";
		 document.getElementById("cookieValue").value = "";
		 
		 // ( ๊ธฐ์–ต ) : ์ฟ ํ‚ค๊ฐ€ ๋งŒ๋“ค์–ด์งˆ๋•Œ ๋„๋ฉ”์ธ(path)๊ฒฝ๋กœ์—์žˆ๋Š” ์–ด๋–ค ํŒŒ์ผ ์š”์ฒญ์ผ์–ด๋‚ ๋•Œ ํ•ด๋‹น ์ฟ ํ‚ค๋ฅผ ์„œ๋ฒ„์— ์ „๋‹ฌ
		 // ๋ธŒ๋ผ์šฐ์ €  [localhost/webPro/javascript/days09] URL ๊ฒฝ๋กœ  ์š”์ฒญํŒŒ์ผ     -> ์š”์ฒญ ->   ์„œ๋ฒ„
		 //                        ์ฟ ํ‚ค๊ฐ’ ์„œ๋ฒ„ ์ „์†ก X                           ์ž๋™  ์ „๋‹ฌ
		 //                        name, age                                          ์ „๋‹ฌ๋œ ์ฟ ํ‚ค๊ฐ’์„ ์‚ฌ์šฉํ•ด์„œ ์„œ๋ฒ„ ์ฒ˜๋ฆฌ.( JSP/Servlet )
	}
	function getAllCookies() {
		// name=admin; age=20
		var cookies =  document.cookie;
		
		document.getElementById("demo").innerHTML = cookies;
	}
	
	function getCookie() {
		var cname = document.getElementById("cookieName").value;
		// "name=admin; age=20"
		var cookies =  document.cookie;
		//  cookies  ๋ฌธ์ž์—ด ํŒŒ์‹ฑ
		var cookieArray = cookies.split(";");   // string or regexp
		for (var i = 0; i < cookieArray.length; i++) {
			 var cnv = cookieArray[i].split("=");
			  // var cname =  cnv[0]; 
			  if(  cname == cnv[0].trim() ){
				  var cvalue =  cnv[1];
				  document.getElementById("cookieValue").value =  unescape( cvalue );
				  break;
			  }
		}
		
	}
	function delCookie() {
	     // ๋งŒ๋ฃŒ์‹œ์ ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ธŒ๋ผ์šฐ์ € ๋‹ซ์œผ๋ฉด ์ฟ ํ‚ค ์ž๋™ ์‚ญ์ œ.
	     // ๊ฐ™์€ ์ฟ ํ‚ค์ด๋ฆ„์œผ๋กœ ์ฟ ํ‚ค ์ƒ์„ฑ +  ๋งŒ๋ฃŒ์‹œ์ ์„ ๊ณผ๊ฑฐ๋กœ ์„ค์ •
	     
	     var cname = document.getElementById("cookieName").value;
    	
    	var now = new Date();  // ์˜ค๋Š˜๋‚ ์งœ + 10 ์ผ ์ถ”๊ฐ€
    	now.setDate(  now.getDate() - 10 );
    	// console.log( now.toLocaleString() );
    	
    	// ์ฟ ํ‚ค์ด๋ฆ„="์ฟ ํ‚ค๊ฐ’"; ๋งŒ๋ฃŒ์‹œ์ ="๋‚ ์งœ"; ๊ฒฝ๋กœ="/"; ๋„๋ฉ”์ธ="๋„๋ฉ”์ธ๋ช…"; ๋ณด์•ˆ(true/false)
		 document.cookie =  cname +  "=; expires=" + now.toUTCString() ;  //  ๋ฌธ์ž์—ด ํ˜•์‹
		 
	}

 

escape ํ•จ์ˆ˜

str 1  :  <input type="text" id="str1"  value="ํ™๊ธธ๋™"><br> 
str 2 :  <input type="text" id="str2"><br> 
<br>

<button onclick="escape_test();"> escape()</button>
<button onclick="unescape_test();"> unescape()</button>
  function escape_test(){
	   var result =  escape( document.getElementById("str1").value ); // ํ•œ๊ธ€ ->  ASCII
	   document.getElementById("str2").value= result;
	   // %uD64D%uAE38%uB3D9
  }
  
  function unescape_test(){
	   var result =  unescape( document.getElementById("str2").value ); // ํ•œ๊ธ€ ->  ASCII
	   document.getElementById("str1").value= result;    
 }

์ฟ ํ‚ค ์˜ˆ์ œ

js ํด๋ž˜์Šค ์ƒ์† X

js ๊ทธ๋ž˜ํ”ฝ X

js ์›น API X

js Ajax + jquery Ajax O

js JSON O

 

์›นํŽ˜์ด์ง€  +  ์„ค์ •( ๊ธ€ํฌ๊ธฐ, ํฐํŠธ ๋“ฑ๋“ฑ )   + ์ƒํƒœ ์œ ์ง€( ์ €์žฅ )  ์„œ๋ฒ„(DB X,  ์„ธ์…˜ X ),   ํด๋ผ์ด์–ธํŠธ ์ƒํƒœ๊ด€๋ฆฐ( ์ฟ ํ‚ค ) O

๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ : 3๊ฐ€์ง€ ์ค‘ ์— ํ•˜๋‚˜๋ฅผ ์„ ํƒ  -> ์ฟ ํ‚ค ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€ ์ €์žฅ  + ๋งŒ๋ฃŒ์‹œ์ 

์›นํŽ˜์ด์ง€ ์š”์ฒญ - ๋งˆ์ง€๋ง‰ ์„ค์ •ํ•œ ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€๋กœ ์„ค์ •...

 

  ์ฟ ํ‚ค์ด๋ฆ„ : bgimg

  ์ฟ ํ‚ค๊ฐ’    : ์ด๋ฏธ์ง€์ด๋ฆ„

 

   <table border="1" style="background-color: white;width:400px;margin:0 auto">
   <tr>
     <td><img src="../images/item01.gif" alt="" /></td>
     <td><img src="../images/item02.gif" alt="" /></td>
     <td><img src="../images/item03.gif" alt="" /></td>
  </tr>
  <tr>
     <td><input type="radio" checked="checked"  name="bgImages" id="item01" value="item01" />item01</td>
     <td><input type="radio" name="bgImages"  id="item02" value="item02" />item02</td>
     <td><input type="radio" name="bgImages" id="item03" value="item03" />item03</td>
  </tr>
  </table>
   var cname = "bgimg";
   var cvalue = "item01";
   var expiresdays = 10;
   
   window.onload = function (){
	   
	   // 1. ์ฟ ํ‚ค์ด๋ฆ„ : bgimg์— ์ €์žฅ๋œ ์ฟ ํ‚ค๊ฐ’(item03)์„ ์ฝ์–ด์™€์„œ
	   
	   cvalue =  getCookie(cname);
       // null              item01
       if( cvalue ){   // ์ฟ ํ‚ค๊ฐ€ ์กด์žฌ O 
            //         ใ„ฑ. ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€ ์ฟ ํ‚ค๊ฐ’์˜ ์ด๋ฏธ์ง€๋กœ ์„ค์ •
            document.body.style.backgroundImage = "url(../images/"+ cvalue+".gif)";
    	   //         ใ„ด. ๋ผ๋””์˜ค๋ฒ„ํŠผ  == ์ฟ ํ‚ค๊ฐ’     ์ฒดํฌ...์„ค์ •
    	   document.getElementById(cvalue).checked = "checked";
       }else{  // null -> false  ์ฟ ํ‚ค๊ฐ€ ์กด์žฌ X
    	   document.body.style.backgroundImage = "url(../images/item01.gif)";
    	   document.getElementById("item01").checked = "checked";
       }
	   
	   
	   // 2. ๋ผ๋””์˜ค ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๋•Œ.
	   //     ใ„ฑ. ์ฒดํฌ๋œ ๋ผ๋””์˜ค ๋ฒ„ํŠผ == value="item02" ๊ฐ’์„ ์ฟ ํ‚ค๋กœ ์ €์žฅ	   
	   //      ใ„ด. ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€ == value="item02" ๊ฐ’์œผ๋กœ ์„ค์ •
	   var rdbs = document.getElementsByName("bgImages");
	   for (let rdb of rdbs) {
		  rdb.onclick = function (){
			    // ใ„ฑ. 
			    cvalue =  event.srcElement.value;
			    setCookie(cname, cvalue, expiresdays);
			    // ใ„ด.
			    document.body.style.backgroundImage = "url(../images/"+ cvalue+".gif)";
		  }
	  } // 
	   
   } // onload
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ