
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>문자열을 유효한 URI(Uniform Resource Identifier UTF-8로 인코딩)로 인코딩/디코딩 한다.</title>
<link rel="stylesheet" href="css/encodeURI_decodeURI.css">
<script type="text/javascript" src="js/encodeURI_decodeURI.js"></script>
</head>
<body>
<div>
<table>
<tr>
<td width="30%">문자열입력(한글)</td>
<td><input type="text" id="inputText" size="70"/> </td>
</tr>
<tr>
<td>URI로 인코딩된 값</td>
<td id="encode_1"></td>
</tr>
<tr>
<td>URI를 디코딩된 값</td>
<td id="decode_1"></td>
</tr>
<tr>
<td><button type="button" id="btnOK_1">변경</button></td>
<td><button type="button" id="btnReset_1">다시</button></td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td width="30%">URI로 인코딩된 값 입력</td>
<td><input type="text" id="encode_2" size="70"/> </td>
</tr>
<tr>
<td>URI를 디코딩된 값</td>
<td id="decode_2"></td>
</tr>
<tr>
<td><button type="button" id="btnOK_2">변경</button></td>
<td><button type="button" id="btnReset_2">다시</button></td>
</tr>
</table>
</div>
</body>
</html>
JS
window.onload = function(){
document.querySelector("input#inputText").focus();
document.querySelector("button#btnOK_1").addEventListener('click', ()=>{
func_encodeURI_decodeURI();
});
document.querySelector("button#btnReset_1").addEventListener('click', ()=>{
document.querySelector("input#inputText").value = "";
document.querySelector("td#encode_1").innerHTML = "";
document.querySelector("td#decode_1").innerHTML = "";
document.querySelector("input#inputText").focus();
});
document.querySelector("input#inputText").addEventListener('keydown', (e) => {
console.log(e.keyCode);
if(e.keyCode == 13) {
func_encodeURI_decodeURI();
}
});
document.querySelector("button#btnOK_2").onclick = () => {
func_decodeURI(document.querySelector("input#encode_2").value);
};
document.querySelector("input#encode_2").onkeydown = (e) => {
if(e.keyCode == 13) {
func_decodeURI(document.querySelector("input#encode_2").value);
}
};
document.querySelector("button#btnReset_2").onclick = () => {
document.querySelector("input#encode_2").value = "";
document.querySelector("input#encode_2").focus();
document.querySelector("td#decode_2").innerHTML = "";
};
}
function func_encodeURI_decodeURI() {
const inputText = document.querySelector("input#inputText").value;
const encodeVal = encodeURI(inputText);
document.querySelector("td#encode_1").innerHTML = encodeVal;
document.querySelector("td#decode_1").innerHTML = decodeURI(encodeVal);
}
function func_decodeURI(encodeURI_value) {
document.querySelector("td#decode_2").innerHTML = decodeURI(encodeURI_value);
}
CSS
@charset "UTF-8";
div {
width: 800px;
margin: 0 auto;
padding: 2%;
}
table {
width: 100%;
border-collapse: collapse;
}
td {
border: solid 1px gray;
height: 50px;
text-align: center;
}
정리
- 09_javascriptStandardObject -> 03_URI
-> encodeURI_decodeURI.html, encodeURI_decodeURI.js, encodeURI_decodeURI.css