본문 바로가기
웹정보구축

HTML 함수 구현

by 할10000 2022. 5. 30.

글쓴이는 프로그램 세계를 C언어로 시작했다. 그래서인지 어떤 언어를 만나도 일단 C언어와 비교하게된다. 

이 언어에는 C에 없는 장/단점이 있구나, 다른 건 더 없나? C언어에선 이랬는데 여기서도 되려나? 이런 식이다. 

최근엔 HTML을 배우기 시작했다. 제작하는 코드가 점점 길어지다보니 이런 생각이 들었다. 여기서도 함수 구역(void()라던가)을 지정해서 코드 구역 식별 및 수정을 간편하게 할 수 없을까? 과연 있었다.  

<!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8" />
 <title> 웹 데이터베이스 </title>
 	<script type="text/JavaScript">
 	var db;
 	window.onload = function()
 	{
 		db = openDatabase("movie", "1.0", "movie database", 1024*1024);
 		db.transaction(exeCreate);
 	}

 	function exeCreate(tx)
 	{
 		tx.executeSql("create table movie(name, actor)");
 	}

 	function insertData()
 	{
 		db.transaction(exeInsert);
 	}

 	function exeInsert(tx)
 	{
 		tx.executeSql("insert into movie values(?,?)", [movie.value, actor.value]);
 	}
    function viewData()
 	{
 		db.transaction(exeSelect);
 	}

    function exeSelect(tx)
 	{
 		tx.executeSql("select * from movie", [], viewTabe);
 	}

 	function viewTabe(tx, rs)
 	{
 		for(var i = 0; i < rs.rows.length; i++)
 		{
 		var row = rs.rows.item(i);
 		document.getElementById("table").innerHTML += "<tr><td>" + row["name"]
		+ "</td><td>" + row["actor"] + "</td></tr>";
 		}
 	}
 	</script>
 </head>
 <body>
 	movie: <input type="text" id="movie"><br>
 	actor: <input type="text" id="actor"><br>
 	<input type="button" onclick="insertData()" value="저장">
 	<input type="button" onclick="viewData()" value="조회"><br>
 	<hr>
 	<table id="table"></table>
 </body>
 </html>

HTML 구현

웹 데이터베이스 movie:
actor: