-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage06.html
176 lines (159 loc) · 7.1 KB
/
page06.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Petfinder API | Page 6</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="css/styles.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<a class="navbar-brand" href="index.html">Home</a></div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav">
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Menu<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="index.html">Home</a></li>
<li><a href="page01.html">1. API Keys and Making Requests</a></li>
<li><a href="page02.html">2. Cross-Domain Requests & JSONP</a></li>
<li><a href="page03.html">3. The pet.get Method & the DOM</a></li>
<li><a href="page04.html">4. Adding Page Content</a></li>
<li><a href="page05.html">5. Adding Images</a></li>
<li><a href="page06.html">6. Getting a Random Cat</a></li>
<li><a href="page07.html">7. Adding a Button and Event Listener</a></li>
<li><a href="page08.html">8. Conclusion</a></li>
</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<div class="container-fluid">
<div class="jumbotron">
<h1>Getting a Random Cat</h1>
<p>Using the Petfinder API's pet.getRandom method to populate the table with a different cat's information upon each load.</p>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12"><p>Next, let's create a widget that generates a table like the one before, but for a random pet. For this, we'll se use the Petfinder API's <strong>pet.getRandom</strong> method.</p>
<pre>function requestJSONP(url) {
var script = document.createElement('script');
script.src = url;
script.onload = function () {
this.remove();
};
var head = document.getElementsByTagName('head')[0];
head.insertBefore(script, head.childNodes[0]);
}
function getKitty(data) {
console.log(data); //test
}
var url = 'http://api.petfinder.com/pet.getRandom?format=json&key=yourkeyhere&animal=cat&location=11215&callback=getKitty';
requestJSONP(url);
</pre>
<img src="images/random_json.png" class="img-responsive" alt="Get Random Pet"><br/>
<p>The ID of the random pet is in <strong>petfinder.petIds.id</strong>. If you refresh the page, you'll notice that the ID is different each time. We can use the ID to call our previous <strong>getKitty</strong> function for the random cat.</p>
<p>This time, we first call the requestJSONP function on a URL formatted for the <strong>pet.GetRandom</strong> method, with the <strong>animal=cat</strong> argument to restrict our search to cats. Within the new <strong>getData</strong> function, we call our previous function to populate the table for that particular cat.</p>
<p>Now when you refresh the page, the table is updated based on the ID in the pet.getRandom response!</p>
<div class="row">
<div class="col-lg-6">
<table>
<tbody>
<tr>
<th scope="col"><h3>Cat of the Day</h3></th>
<th scope="col"> </th>
</tr>
<tr>
<td colspan="2" id="image"> </td>
</tr>
<tr>
<td><p>Name:</p></td>
<td id="name"> </td>
</tr>
<tr>
<td><p>Age:</p></td>
<td id="age"> </td>
</tr>
<tr>
<td><p>Sex:</p></td>
<td id="sex"> </td>
</tr>
<tr>
<td><p>Breed:</p></td>
<td id="breed">
<ul id = "breedlist"></ul>
</td>
</tr>
<tr>
<td><p>City:</p></td>
<td id="city"> </td>
</tr>
</tbody>
</table></div>
<div class="col-lg-6"><pre>
function getKitty(data) {
var response = data.petfinder.pet;
var image = new Image();
image.src = response.media.photos.photo[3].$t;
image.setAttribute('class', 'img-responsive');
image.setAttribute('alt', response.name.$t);
document.getElementById('image').appendChild(image);
document.getElementById('name').textContent = response.name.$t;
document.getElementById('age').textContent = response.age.$t;
document.getElementById('sex').textContent = response.sex.$t;
for(var i = 0; i < response.breeds.breed.length; i++){
var newItem = document.createElement("li");
var textNode = document.createTextNode(response.breeds.breed[i].$t);
newItem.appendChild(textNode);
document.getElementById('breedlist').appendChild(newItem);
}
document.getElementById('city').textContent = response.contact.city.$t + ', ' + response.contact.state.$t;
}
function getID(data) {
var petID = data.petfinder.petIds.id.$t;
var petURL = baseURL + 'pet.get?format=json&key=' + key + '&id=' + petID + '&callback=getKitty';
requestJSONP(petURL);
}
var url = baseURL+ 'pet.getRandom?format=json&key=' + key + '&animal=cat&callback=getID';
requestJSONP(url);
</pre></div>
</div>
<hr>
<div class="row">
<div class="col-lg-4">
<a href="page05.html"><button type="button" class="btn btn-info">Prev.</button></a>
<a href="page07.html"><button type="button" class="btn btn-info">Next</button></a>
</div>
</div>
<hr>
<div class="row">
<div class="text-center col-md-6 col-md-offset-3">
<p>Copyright © Morgan Brenner · 2016 </p>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="js/credentials.js"></script>
<script src="js/p6_ex.js"></script>
</body>
</html>