56 lines
1.8 KiB
HTML
56 lines
1.8 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html lang="en">
|
||
|
<head>
|
||
|
<meta charset="UTF-8">
|
||
|
<title>Random Animal Generator</title>
|
||
|
</head>
|
||
|
<body>
|
||
|
<h1>Random Animal Generator</h1>
|
||
|
<p id="animal-name">Click a button to get a random animal:</p>
|
||
|
<button id="button1">Button 1</button>
|
||
|
<button id="button2">Button 2</button>
|
||
|
|
||
|
<script>
|
||
|
// Function to fetch a random animal name from the server
|
||
|
async function getRandomAnimal() {
|
||
|
try {
|
||
|
const response = await fetch('/getRandomAnimal');
|
||
|
const data = await response.json();
|
||
|
document.getElementById('animal-name').textContent = data.animalName;
|
||
|
} catch (error) {
|
||
|
console.error('Error fetching data:', error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Add click event listeners to the buttons
|
||
|
document.getElementById('button1').addEventListener('click', () => {
|
||
|
recordButtonClick('Button 1');
|
||
|
});
|
||
|
|
||
|
document.getElementById('button2').addEventListener('click', () => {
|
||
|
recordButtonClick('Button 2');
|
||
|
});
|
||
|
|
||
|
// Function to record button clicks on the server
|
||
|
async function recordButtonClick(buttonName) {
|
||
|
try {
|
||
|
await fetch('/recordButtonClick', {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
'Content-Type': 'application/json',
|
||
|
},
|
||
|
body: JSON.stringify({ buttonName }),
|
||
|
});
|
||
|
getRandomAnimal(); // Load another random animal
|
||
|
} catch (error) {
|
||
|
console.error('Error recording button click:', error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Initial random animal load
|
||
|
getRandomAnimal();
|
||
|
</script>
|
||
|
</body>
|
||
|
</html>
|
||
|
|