[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">×</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">×</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">×</span>
var spanbtn = document.createElement("span");
spanbtn.className = "closebtn";
// document.createTextNode(content)
spanbtn.innerHTML = "×";
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">×</span>
var spanbtn = document.createElement("span");
spanbtn.className = "closebtn";
// document.createTextNode(content)
spanbtn.innerHTML = "×";
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
์ต๊ทผ๋๊ธ