수업소개
글목록을 ajax로 구현하는 방법을 알아봅니다.
강의1
소스코드
index.html
<!doctype html> <html> <head> <title>WEB1 - Welcome</title> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="colors.js"></script> </head> <body> <h1><a href="#!welcome">WEB</a></h1> <input id="night_day" type="button" value="night" onclick=" nightDayHandler(this); "> <ol id="nav"> </ol> <article> </article> <script> function fetchPage(name){ fetch(name).then(function(response){ response.text().then(function(text){ document.querySelector('article').innerHTML = text; }) }); } if(location.hash){ fetchPage(location.hash.substr(2)); } else { fetchPage('welcome'); } fetch('list').then(function(response){ response.text().then(function(text){ document.querySelector('#nav').innerHTML = text; }) }); </script> </body> </html>
강의2
소스코드
index.html
<!doctype html> <html> <head> <title>WEB1 - Welcome</title> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="colors.js"></script> </head> <body> <h1><a href="#!welcome">WEB</a></h1> <input id="night_day" type="button" value="night" onclick=" nightDayHandler(this); "> <ol id="nav"> </ol> <article> </article> <script> function fetchPage(name){ fetch(name).then(function(response){ response.text().then(function(text){ document.querySelector('article').innerHTML = text; }) }); } if(location.hash){ fetchPage(location.hash.substr(2)); } else { fetchPage('welcome'); } fetch('list').then(function(response){ response.text().then(function(text){ var items = text.split(','); var i = 0; var tags = ''; while(i<items.length){ var item = items[i]; item = item.trim(); var tag = '<li><a href="#!'+item+'" onclick="fetchPage(\''+item+'\')">'+item+'</a></li>'; tags = tags + tag; i = i + 1; } document.querySelector('#nav').innerHTML = tags; }) }); </script> </body> </html>
list
html,css,javascript,ajax
ajax
<h2>Ajax</h2> Ajax (also AJAX; /ˈeɪdʒæks/; short for "Asynchronous JavaScript + XML")[1][2] is a set of Web development techniques using many Web technologies on the client side to create asynchronous Web applications. With Ajax, Web applications can send and retrieve data from a server asynchronously (in the background) without interfering with the display and behavior of the existing page. By decoupling the data interchange layer from the presentation layer, Ajax allows Web pages, and by extension Web applications, to change content dynamically without the need to reload the entire page.[3] In practice, modern implementations commonly utilize JSON instead of XML due to the advantages of JSON being native to JavaScript.[4]