-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkingForm.html
More file actions
99 lines (74 loc) · 2.52 KB
/
workingForm.html
File metadata and controls
99 lines (74 loc) · 2.52 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Film search machine</h1>
<p hidden>Hello!!!</p>
<div class="main">
<form onsubmit="return false;" id="form1">
<input type="text" id="input1" value="batman">
<button class="mdc-button--raised" type="submit"> Search!</button>
</form>
</div>
<div class="main" id="result"></div>
<script>
'use strict';
//Вешаем обработчик на форму
document.getElementById('form1').addEventListener('submit', getInputVal)
function getInputVal(event) {
let msg = document.getElementById('input1').value;
return modifyValToUrl(msg)
}
function httpGet(url, arrStr) {
return new Promise(function(resolve, reject) {
let xhr = new XMLHttpRequest();
//alert (arrStr);
let urlStr = `${url}${arrStr}`
xhr.open('GET', urlStr, true);
xhr.onload = function() {
if (this.status == 200) {
resolve(this.response);
} else {
let error = new Error(this.statusText);
error.code = this.status;
reject(error);
}
};
xhr.onerror = function() {
reject(new Error("Network Error"));
};
xhr.send();
});
}
function modifyValToUrl(msg) {
let arr = msg.split(' ');
let arrExt = arr.map(function(i) {
return i;
});
var arrStr = arrExt.join('?');
httpGet('https://www.omdbapi.com/?apikey=adac724d&t=', arrStr)
.then(response => {
let filmObj = JSON.parse(response);
return filmObj;
})
.then(filmObj => {
let poster1 = filmObj.Poster;
console.log(poster1);
let div = document.createElement('div');
div.className = 'film';
div.innerHTML = ` Title: <strong> ${filmObj.Title}</strong> <br>
Year: ${filmObj.Year}<br>
Country: ${filmObj.Country}<br>
Genre: ${filmObj.Genre} <br>
Released: ${filmObj.Released} <br>
Plot: ${filmObj.Plot} <br>
<img class = "main main-img" src = ${poster1}>
`;
document.body.appendChild(div);
});
};
</script>
</body>
</html>