-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
86 lines (67 loc) · 1.77 KB
/
weather.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
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
$(document).ready(function() {
$("#search").hide();
$("#search_icon").click(function() {
$("#search").toggle("slow");
$("#search").focus();
// $("#search_icon").hide();
});
$("#search").keypress(function(e) {
if (e.which == 13) {
var item = $("#search").val();
console.log("Key pressed");
$.ajax({
url: "https://en.wikipedia.org/w/api.php",
jsonp: "callback",
dataType: 'jsonp',
data: {
action: "query",
list: "search",
srsearch: item,
srinfo: "suggestion",
srlimit: "10",
format: "json"
},
xhrFields: {
withCredentials: true
},
success: displayResult
});
}
});
function displayResult(data) {
$('#results td').remove();
$.each(data.query.search, function(index, value) {
var strTitle = value.title;
var strSnippet = value.snippet;
$('#results').append('<tr><td><a>' + strTitle + '</a></tr><<tr><td>' + strSnippet + '</td></tr><td><hr></td>');
});
}
$("div").on("click", "#results tr", function(e) {
var item = $("#search").val();
var heading = e.target.innerText;
$.ajax({
url: "https://en.wikipedia.org/w/api.php",
jsonp: "callback",
dataType: 'jsonp',
data: {
action: "query",
generator: "search",
gsrsearch: item,
prop: "info",
inprop: "url",
format: "json"
},
xhrFields: {
withCredentials: true
},
success: linkResult
});
function linkResult(data) {
$.each(data.query.pages, function(index, value) {
if (value.title === heading) {
window.open(value.canonicalurl, 'target_blank');
}
});
}
});
});