수업소개
글목록을 ajax로 구현하는 방법을 알아봅니다.
강의1
소스코드
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!doctype html> < html > < head > < title >WEB1 - Welcome</ title > < meta charset = "utf-8" > < 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <!doctype html> < html > < head > < title >WEB1 - Welcome</ title > < meta charset = "utf-8" > < 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
1 | html,css,javascript,ajax |
ajax
1 2 | <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] |