Program 10
Program 10
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Load Text File Content with AJAX</h1>
<button id="loadPlainJs">Load with Plain JS</button>
<button id="loadJQuery">Load with jQuery</button>
<div id="content"></div>
<script src="script.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</body>
</html>
document.getElementById('loadPlainJs').addEventListener('click', function() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("content").innerHTML = this.responseText;
}
};
document.getElementById('loadJQuery').addEventListener('click', function() {
$.ajax({
url: "textfile.txt",
method: "GET",
success: function(data) {
$("#content").html(data);...
},
error: function() {
$("#content").html("An error occurred while loading the file.");
}
});
});