GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Web-Learning/JavaScript/API/ajax-start-XHR.html at master · Horuskov/Web-Learning Expand file tree/ Copy pathajax-start-XHR.htmlMore file actions 102 lines (83 loc) · 2.88 KB / Copy pathajax-start-XHR.htmlFile metadata and controls102 lines (83 loc) · 2.88 KB 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width"> <title>Ajax starting point</title> <style> html, pre { font-family: sans-serif; } body { width: 500px; margin: 0 auto; background-color: #ccc; } pre { line-height: 1.5; letter-spacing: 0.05rem; padding: 1rem; background-color: white; } label { width: 200px; margin-right: 33px; } select { width: 350px; padding: 5px; } </style> <!--[if lt IE 9]> <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script> <![endif]--> </head> <body> <h1>Ajax starting point</h1> <form> <label for="verse-choose">Choose a verse</label> <select id="verse-choose" name="verse-choose"> <option>Verse 1</option> <option>Verse 2</option> <option>Verse 3</option> <option>Verse 4</option> </select> </form> <h2>The Conqueror Worm, <em>Edgar Allen Poe, 1843</em></h2> <pre> </pre> <script> var verseChoose = document.querySelector('select'); var poemDisplay = document.querySelector('pre'); verseChoose.onchange = function () { // every time the select value changes, take it and pass it to the updateDisplay function var verse = verseChoose.value; updateDisplay(verse); } function updateDisplay(verse) { // take the verse as parameter (select value) and take it through typographic changes verse = verse.replace(" ", ""); verse = verse.toLowerCase(); // store the URL name into a variable var url = 'verse database/' + verse + '.txt'; // create a new XHR request stored in a variable var request = new XMLHttpRequest(); // specify the HTTP request method and set the URL request.open('GET', url); // set the type of response expected (XHR return text by default though) request.responseType = 'text' // when reponse has returned/loaded fire the function / load event fire when the whole page has loaded request.onload = function () { // update the <pre> with the reponse of the request (which was specified in text format) poemDisplay.textContent = request.response; }; request.send(); } // load verse 1 by default and make sure the <select> element always shows the correct value updateDisplay('Verse 1'); verseChoose.value = 'Verse 1'; </script> </body> </html> |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |