Problem Overview
I am developing a web app that requires users to enter URLs in a text box. My goal is to check the HTTP status codes for these URLs directly from the frontend.
Concerns
I’m uncertain about the feasibility of this task due to cross-origin resource sharing (CORS) policies and potential security issues in browsers that might prevent me from accessing the data.
Sample Code
function assessWebsiteStatus(inputUrl) {
// This represents my intended functionality
fetch(inputUrl)
.then(res => {
console.log('HTTP Status:', res.status);
return res.status;
})
.catch(err => {
console.log('An error occurred:', err);
});
}
// Handling user input
document.getElementById('urlInput').addEventListener('blur', function() {
const enteredUrl = this.value;
if (enteredUrl) {
assessWebsiteStatus(enteredUrl);
}
});
Queries
- Is it possible to fetch HTTP status codes from websites using pure JavaScript or jQuery?
- What are the possible solutions to bypass CORS issues when checking if a site is reachable?
- How can I best ensure that the URL entered by a user is accessible?
Any advice or other approaches would be greatly appreciated. Thank you!