-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (55 loc) · 1.6 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
52
53
54
55
56
57
58
59
function register()
{
alert("Registration succesful!")
setTimeout(function()
{
window.location.href="Books.html"
},1000)
return false;
}
function login(event)
{
event.preventDefault();
alert("Logged in")
setTimeout(function()
{
window.location.href="Books.html"
},100)
}
async function searchBooks()
{
const searchQuery = document.getElementById('searchInput').value
const apiUrl = `https://www.googleapis.com/books/v1/volumes?q=${searchQuery}`
try
{
const response=await fetch(apiUrl)
const data = await response.json()
displayResults(data.items)
}
catch(error)
{
console.error('Error fetching data:',error)
}
}
function displayResults(books)
{
const bookResults=document.getElementById("bookResults")
bookResults.innerHTML=""
books.forEach(book => {
const title=book.volumeInfo.title
const authors=book.volumeInfo.authors ? book.volumeInfo.authors.join(',') : 'Unknown author'
const thumbnail = book.volumeInfo.imageLinks ? book.volumeInfo.imageLinks.thumbnail : 'https://via.placeholder.com/150'
const bookLink = book.volumeInfo.infoLink;
const bookElement = document.createElement('div');
bookElement.classList.add('book');
bookElement.innerHTML = `
<a href ="${bookLink}" target="blank">
<img src="${thumbnail}" alt="${title}">
<div>
<h2>${title}</h2>
<p>By: ${authors}</p>
</div>
`;
bookResults.appendChild(bookElement);
});
}