-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (44 loc) · 1.94 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
44
45
46
47
48
49
50
51
$('#bar').hide();
$('#spinner').hide();
var xhttp = new XMLHttpRequest();
function strUcFirst(a){return (a+'').charAt(0).toUpperCase()+a.substr(1);}
function clearSearch() {
if(document.getElementById("searchForm").value == ""){
var search ="color";
loadSearch(search);
}
}
function loadSearch(search) {
$('#bar').show();
$('#spinner').show();
if(search == "search"){
var search = document.getElementById("searchForm").value;
}
document.getElementById("name").textContent=search;
var url = "https://api.dictionaryapi.dev/api/v2/entries/en/" + search;
xhttp.open("GET", url, true);
xhttp.send();
var imgUrl = "https://source.unsplash.com/featured/?"+ search;
document.getElementById("searchImage").innerHTML = "<img src='"+imgUrl+"' loading='lazy' class='img-thumbnail'></img>"
}
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("definition").innerHTML = " ";
var response = JSON.parse(xhttp.responseText);
document.getElementById("name").innerHTML = strUcFirst(response["0"].word);
i = 0;
while(i != response[0].meanings[0].definitions.length) {
$('#bar').hide();
$('#spinner').hide();
document.getElementById("definition").innerHTML +="<div id='definitions' class='definition'><i class='bi bi-caret-right'></i>"+strUcFirst(response['0']['meanings']['0']['definitions'][i].definition)+"</div></hr>"
i += 1;
}
}
else if(xhttp.status != 200) {
$('#bar').hide();
$('#spinner').hide();
document.getElementById("definition").innerHTML = "<h2>No meaning found</h2>";
var imgUrl = "https://source.unsplash.com/featured/?404";
document.getElementById("searchImage").innerHTML = "<img src='"+imgUrl+"'class='img-thumbnail' crossorigin='anonymous'></img>"
}
}