GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Web-Learning/JavaScript/API/ajax-start-FetchAPI.html at master · Horuskov/Web-Learning

Expand file tree

/

ajax-start-FetchAPI.html

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
108 lines (89 loc) · 3.36 KB
/

ajax-start-FetchAPI.html

Copy path

File metadata and controls

108 lines (89 loc) · 3.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
103
104
105
106
107
108
<!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';
// fetch the resources located at the url then run the function when it is done (response is received). Fetch gives automatically response as parameter to the promise attached to it
fetch(url).then(
// response is by default name like this but could be 'Geromino'... does not matter that much
function (response) {
// reponse object has a .text() method that turn the data contains into the reponse into raw text
response.text().then(
// tells what we want to do with the raw text when its transformation is done in the previous step
function (text) {
// set <pre> content with the raw text
poemDisplay.textContent = text;
});
});
}
// Another way of writing the code above // return statement is included to get it to pass its result on to the next link in the chain.
// fetch(url).then(function(response) {
// return response.text()
// }).then(function(text) {
// poemDisplay.textContent = text;
// });
// 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.