-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (38 loc) · 1.29 KB
/
index.js
File metadata and controls
39 lines (38 loc) · 1.29 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
$(document).ready(function() {
$('#search').focus();
$('#search').off('keyup');
$('#search').on('keyup', function() {
ajaxCall();
$('iframe').attr('src', '');
});
$('.random').on('click', function() {
randomFunction();
$(this).text('Show me another random article!');
});
});
function ajaxCall() {
$.ajax({
url: 'https://www.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch='+$('#search').val(),
dataType: 'jsonp',
type: 'GET',
success: function(data) {
$('#update').empty();
var data = JSON.stringify(data);
data = JSON.parse(data);
var output = '';
data.query.search.forEach(function(data) {
var title = '<h2>'+data.title+'</h2><br>';
var snippet = '<p>'+data.snippet+'</p>';
var url = '<a href="https://en.wikipedia.org/wiki/'+data.title+'" target="_blank">';
var endUrl = '</a>';
output += url + title + endUrl + snippet + '<hr>';
});
$('#update').append(output);
}
});
}
function randomFunction() {
$('#update').empty();
$('#search').val('');
$('iframe').attr('src', 'https://en.wikipedia.org/wiki/Special:Random');
}