Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ex3 finished
  • Loading branch information
amirhshad committed Dec 14, 2020
commit 322895e5d7690c22bc7378de55b44407c2f76402
57 changes: 57 additions & 0 deletions Week1/homework/js-exercises/dogGallery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const XMLBtn = document.getElementById('btn1');
const AxiosBtn = document.getElementById('btn2');

function xhrMethod() {
const xhr = new XMLHttpRequest();
const li = document.createElement('li');
const image = document.createElement('img');
const ul = document.getElementById('list');
const url = 'https://dog.ceo/api/breeds/image/random'
xhr.open('GET', url);
xhr.send();
xhr.onreadystatechange = processRequest;

ul.appendChild(li.appendChild(image))
Comment thread
amirhshad marked this conversation as resolved.

function processRequest(e) {
if(xhr.readyState == 4 && xhr.status == 200) {
let response = JSON.parse(xhr.responseText);
image.src = response.message;
image.style.width = '300px'
image.style.height = '300px'

};
};
xhr.onerror = function() {
if (xhr.status != 200) {
alert (`Error ${xhr.status}: ${xhr.statusText}`);
};
};

};

XMLBtn.addEventListener('click', xhrMethod);



function axiosMethod() {
const li = document.createElement('li');
const image = document.createElement('img');
const ul = document.getElementById('list');
ul.appendChild(li.appendChild(image))
axios
.get('https://dog.ceo/api/breeds/image/random')
.then(function(response) {
Comment thread
amirhshad marked this conversation as resolved.

image.src = response.data.message;
image.style.width = '300px'
image.style.height = '300px'

})
.catch(function(error) {
console.log(error)
});
}


AxiosBtn.addEventListener('click', axiosMethod);
20 changes: 20 additions & 0 deletions Week1/homework/js-exercises/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dog Gallery</title>
</head>
<body>
<div class="container">
<button id="btn1">XMLHttpRrquest</button>
<button id="btn2">Axios</button>
<ul id="list"></ul>

</div>


<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="dogGallery.js"></script>
</body>
</html>