Skip to content
Open
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
exercise 3
  • Loading branch information
spiropoulos94 committed Feb 10, 2020
commit 727f9ccf1a4268afa76eb01900ddaf8eeaa14bd3
58 changes: 58 additions & 0 deletions Week1/js-exercises-week1/exercise3 dog photo gallery/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//made using xmlhttp request

let xmlObject = new XMLHttpRequest();

function randomDogPhotoXml() {
xmlObject.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
// Typical action to be performed when the document is ready:
let jsonObject = JSON.parse(xmlObject.responseText);
let button = document.querySelector('button');

let image = document.createElement('img');
image.setAttribute('width', '150px');
image.setAttribute('height', '150px');
image.src = jsonObject.message;

document.body.appendChild(image);

console.log(image);
}
};

xmlObject.open('GET', 'https://dog.ceo/api/breeds/image/random');
xmlObject.onerror = err => {
console.log(err);
};
xmlObject.send();
}

let buttonOne = document.getElementById('buttonOne');
buttonOne.addEventListener('click', () => {
randomDogPhotoXml();
});

//made using axios

function randomDogPhotoAxios() {
axios
.get('https://dog.ceo/api/breeds/image/random')
.then(function(response) {
// handle success
let imageAxios = document.createElement('img');
imageAxios.src = response.data.message;
imageAxios.setAttribute('width', '300px');
imageAxios.setAttribute('height', '300px');
console.log(imageAxios);
document.body.appendChild(imageAxios);
})
.catch(function(error) {
// handle error
console.log(error);
});
}

let buttonTwo = document.getElementById('buttonTwo');
buttonTwo.addEventListener('click', () => {
randomDogPhotoAxios();
});
19 changes: 19 additions & 0 deletions Week1/js-exercises-week1/exercise3 dog photo gallery/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
<button id="buttonOne">
click me to add a random dog photo with XMLHttpRequest
</button>
<button id="buttonTwo">
click me to add a random dog photo with Axios
</button>

<script src="app.js"></script>
</body>
</html>