Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e1d35e3

Browse files
done homework JS3W1
1 parent f1be8ae commit e1d35e3

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//function with XMLHTTPREQUEST
2+
3+
function ranImg() {
4+
try{
5+
let xhttp = new XMLHttpRequest();
6+
xhttp.onreadystatechange = function() {
7+
if (this.readyState == 4 && this.status == 200) {
8+
9+
let myUl=document.createElement('ul');
10+
let myLi=document.createElement('li');
11+
let myImg=document.createElement('img');
12+
let myDiv=document.getElementById('btn');
13+
myImg.src=((JSON.parse(xhttp.responseText)).message);
14+
myImg.style.width="200px";
15+
myImg.style.height="200px";
16+
myLi.appendChild(myImg);
17+
myUl.appendChild(myLi);
18+
myDiv.appendChild(myUl);
19+
20+
}
21+
};
22+
xhttp.open("GET", "https://dog.ceo/api/breeds/image/random", true);
23+
xhttp.send();
24+
}catch(e){console.log("une erreur s'est produite");}
25+
}
26+
27+
28+
//function with AXIOS
29+
30+
async function ranImg1() {
31+
32+
try{
33+
const response = await axios.get('https://dog.ceo/api/breeds/image/random');
34+
let myUl=document.createElement('ul');
35+
let myLi=document.createElement('li');
36+
let myImg=document.createElement('img');
37+
let myDiv=document.getElementById('btn');
38+
myImg.src=response.data.message;
39+
myImg.style.width="200px";
40+
myImg.style.height="200px";
41+
myLi.appendChild(myImg);
42+
myUl.appendChild(myLi);
43+
myDiv.appendChild(myUl);
44+
}catch{
45+
console.log("une erreur c'est produite");
46+
}
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=\, initial-scale=1.0">
6+
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
11+
<h2>Randomized dog photo gallery</h2>
12+
13+
<div id="btn">
14+
<button type="button" onclick="ranImg()">Change Content</button>
15+
<button type="button" onclick="ranImg1()">Change Content</button>
16+
</div>
17+
18+
<script src="dogPhoto.js">
19+
20+
21+
</script>
22+
23+
</body>
24+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
8+
<title>Document</title>
9+
</head>
10+
<body>
11+
12+
13+
14+
<script src="programmerHummor.js" >
15+
16+
</script>
17+
</body>
18+
</html>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//function with XMLHTTPREQUEST
2+
3+
logApiHummor();
4+
function logApiHummor(){
5+
var xhttp;
6+
var myData;
7+
var image = document.createElement("img");
8+
9+
var body=document.body;
10+
11+
try{
12+
xhttp = new XMLHttpRequest();
13+
}catch(e){
14+
try{
15+
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
16+
}catch(e){console.log("Erreur")}
17+
}
18+
try{
19+
xhttp.onreadystatechange = function() {
20+
if (xhttp.readyState == 4 && xhttp.status == 200) {
21+
myData=(JSON.parse(xhttp.responseText)[0]);
22+
image.src=myData.url;
23+
console.log(myData);
24+
}
25+
};
26+
xhttp.open("GET","https://jsonplaceholder.typicode.com/photos", true);
27+
xhttp.send();
28+
}catch{
29+
console.log("une erreur c'est produite");
30+
}
31+
body.appendChild(image);
32+
}
33+
34+
35+
//function with axios
36+
37+
(async function logApiHummor(){
38+
var image = document.createElement("img");
39+
try{
40+
const response = await axios.get('https://jsonplaceholder.typicode.com/photos')
41+
console.log(response.data[0]);
42+
image.src=response.data[0].url;
43+
}catch{
44+
console.log("une erreur c'est produite");
45+
}
46+
47+
48+
var body=document.body;
49+
body.appendChild(image);
50+
})();
51+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//function with XMLHTTPREQUEST
2+
3+
logApiData1();
4+
function logApiData1(){
5+
var xhttp;
6+
try{
7+
xhttp = new XMLHttpRequest();
8+
}catch(e){
9+
try{
10+
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
11+
}catch(e){console.log("Erreur")}
12+
}
13+
try{
14+
xhttp.onreadystatechange = function() {
15+
if (xhttp.readyState == 4 && xhttp.status == 200) {
16+
console.log((JSON.parse(xhttp.responseText))[0]);
17+
}
18+
};
19+
xhttp.open("GET","https://jsonplaceholder.typicode.com/users", true);
20+
xhttp.send();
21+
}catch{
22+
console.log("une erreur c'est produite");
23+
}
24+
}
25+
26+
//function with axios
27+
28+
const axios = require ('axios');
29+
(async function logApiData1(){
30+
try{
31+
const response = await axios.get('https://jsonplaceholder.typicode.com/users')
32+
console.log(response.data[0]);
33+
}catch{
34+
console.log("une erreur c'est produite");
35+
}
36+
})()
37+

0 commit comments

Comments
 (0)