-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikiviewer.js
46 lines (40 loc) · 1.3 KB
/
wikiviewer.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
44
45
46
$(document).ready(function(){
var urlWiki;
var userForm;
var userKeyword;
$("#searchSubmit").click(function(){
var userKeyword = $("#searchKey").val();
urlWiki =
"https://en.wikipedia.org/w/api.php" +
"?" +
$.param({
action: "opensearch",
search: userKeyword,
prop: "extracts",
format: "json",
limit: 10
}) +
"&callback=?"; //that's for jsonp
$.ajax({
type: "GET",
dataType: "json", //next time don't forget the Cap on Type, it's not datatype but dataType!!!
url: urlWiki,
success: function(data) {
console.log(data[1]); //it WORKS!!!!
for (var x = 0; x < data[1].length; x++) {
//$("#test").html("<div><h2>"+ data[1][x] + "</h2></div>"); //works
// $('<div class="results" />').text(arrayVariable[i]).appendTo('body');
// $("<div class='searchResult />").text(data[1][x]).appendTo("#test"); //doesn't work
$("#top").html('<p>Results for "' + userKeyword + '":</p>')
$("#content").append($("<div/>", { id: "result" + x, class : "searchResult"}));
$("#result" + x).html("<a href='"+ data[3][x] +"' target=_blank>"+"<h1 class='resultTitle'>" + data[1][x] + "</h1>" + "<p>" + data[2][x]+ "</p></a>");
}
} //fin de success
}) //fin de l'ajax
})
$("#searchKey").keypress(function(e){
if(e.which==13) {
$("#searchSubmit").click();
}
})
})