-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (42 loc) · 1.42 KB
/
index.js
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
function ajaxCall(){
$.ajax({
url: 'https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=' + $('#search').val(),
dataType: 'jsonp',
type: 'GET',
success: function(data) {
// console.log(data);
$('#update').empty();
var data = JSON.stringify(data);
data = JSON.parse(data);
//console.log(data.query.search);
// loop through data and output
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').empty();
$('iframe').attr('src','https://en.wikipedia.org/wiki/Special:Random');
}
$(document).ready(function(){
$('#search').focus();
$('#search').off('keyup');
$('#search').on('keyup',function(){
ajaxCall();
$('iframe').attr('src','');
});
//show random wiki Article
$('.random').on('click',function(){
randomFunction();
$(this).text('Show me another random article!');
});
});