-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoive.js
More file actions
69 lines (50 loc) · 2.3 KB
/
moive.js
File metadata and controls
69 lines (50 loc) · 2.3 KB
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
function search(event) {
event.preventDefault();
var movieName = document.getElementById("movie-name").value;
var content = document.getElementById("content");
const htmlRequest = new XMLHttpRequest();
const link = "http://www.omdbapi.com/?apikey=e7e1bceb&t=" + encodeURIComponent(movieName);
htmlRequest.open('GET', link, true);
const loadingMessage = document.createElement("p");
loadingMessage.innerText = "Loading...";
content.appendChild(loadingMessage);
htmlRequest.onload = function() {
if (htmlRequest.status >= 200 && htmlRequest.status < 300) {
const response = JSON.parse(htmlRequest.responseText);
if (!response.Title) {
content.innerHTML = "Movie not found!";
return;
}
content.innerHTML = '';
const title = document.createElement("h2");
title.innerText = response.Title;
const year = document.createElement("p");
year.innerText = "Release Year: " + response.Year;
const director = document.createElement("p");
director.innerText = "Director: " + response.Director;
const language = document.createElement("p");
language.innerText = "Language: " + response.Language;
const runtime = document.createElement("p");
runtime.innerText = "Runtime: " + response.Runtime;
const rating = document.createElement("p");
rating.innerText = "Rating: " + response.imdbRating;
const imgTag = document.createElement("img");
imgTag.src = response.Poster;
imgTag.alt = response.Title + " movie poster";
content.appendChild(title);
content.appendChild(year);
content.appendChild(director);
content.appendChild(language);
content.appendChild(runtime);
content.appendChild(rating);
content.appendChild(imgTag);
document.title = response.Title;
} else {
content.innerHTML = 'Error: ' + htmlRequest.statusText;
}
};
htmlRequest.onerror = function() {
content.innerHTML = 'Failed to fetch movie data. Please try again.';
};
htmlRequest.send();
}