Skip to content

Instantly share code, notes, and snippets.

@akhyaruu
Last active September 12, 2019 15:25
Show Gist options
  • Save akhyaruu/ca9c5bd43cefd16e0a8a1b7279ad67b9 to your computer and use it in GitHub Desktop.
Save akhyaruu/ca9c5bd43cefd16e0a8a1b7279ad67b9 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Index</title>
</head>
<body>
<button id="button">Dapatkan text file</button>
<br>
<div id="text"></div>
<script>
document.getElementById('button').addEventListener('click', loadText);
function loadText() {
let xhr = new XMLHttpRequest();
xhr.open('GET', 'sample.txt', true);
xhr.onload = function () {
if (this.status == 200) {
document.getElementById('text').innerHTML = this.responseText;
} else if (this.status == 404) {
document.getElementById('text').innerHTML = 'Not Found';
}
}
xhr.send();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment