Calculator Tools Calculator Tools
Create

Country Information App

Dec 19, 2023

About this app

Type a Country, and get its capital, language, UN status, and population.

Country Information App Country Information App Type a Country: Get Information Learn more about the country

Related apps

Put this on your site

Source

                    <html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Country Information App</title>
<meta name="description" content="Type a Country, and get its capital, language, UN status, and population.">
<meta name="keywords" content="country, information, capital, language, UN status, population">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script type="text/javascript">
try {
// App Javascript Goes Here. Place your entire script content inside the try block for error handling.
document.addEventListener("DOMContentLoaded", function () {
const submitButton = document.getElementById('submit-button');
const countryInput = document.getElementById('country-input');
const resultContainer = document.getElementById('result-container');
const errorContainer = document.getElementById('error-container');
const countryLink = document.getElementById('country-link');

submitButton.addEventListener('click', function () {
const country = countryInput.value.trim();
if (country !== '') {
errorContainer.style.display = 'none';
resultContainer.innerHTML = '<p>Loading...</p>';

fetch(`https://restcountries.com/v3/name/${country}`)
.then(function (response) {
return response.json();
})
.then(function (data) {
if (data && data.length > 0) {
const countryData = data[0];
const capital = countryData.capital ? countryData.capital : 'N/A';
const language = countryData.languages ? countryData.languages[0] : 'N/A';
const unStatus = countryData.unStatus ? countryData.unStatus : 'N/A';
const population = countryData.population ? countryData.population : 'N/A';

const resultHtml = `
<p><strong>Capital:</strong> ${capital}</p>
<p><strong>Language:</strong> ${language}</p>
<p><strong>UN Status:</strong> ${unStatus}</p>
<p><strong>Population:</strong> ${population}</p>
`;
resultContainer.innerHTML = resultHtml;

countryLink.href = `https://en.wikipedia.org/wiki/${country.replaceAll(' ', '_')}`;
countryLink.style.display = 'block';
} else {
resultContainer.innerHTML = '';
countryLink.style.display = 'none';
errorContainer.innerText = 'Country not found!';
errorContainer.style.display = 'block';
}
})
.catch(function () {
resultContainer.innerHTML = '';
countryLink.style.display = 'none';
errorContainer.innerText = 'Error occurred while fetching data.';
errorContainer.style.display = 'block';
});
}
});
});

} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>

<style>
/* App CSS Goes Here */
#main-container {
margin-top: 50px;
}

#result-container {
margin-top: 20px;
}

#error-container {
color: red;
font-weight: bold;
display: none;
}

#country-link {
display: none;
margin-top: 10px;
text-align: center;
}
</style>
</head>
<body>
<div id="main-container" class="container">
<h2>Country Information App</h2>
<div class="mb-3">
<label for="country-input" class="form-label">Type a Country:</label>
<input type="text" class="form-control" id="country-input" placeholder="Enter a country name">
</div>
<button id="submit-button" class="btn btn-primary">Get Information</button>
<div id="result-container"></div>
<div id="error-container"></div>
<a id="country-link" href="" target="_blank">Learn more about the country</a>
</div>
</body>
</html>