check if file exists javascript
<!DOCTYPE HTML>
<html>
<head>
<title>
How to check if file exist or
not on HTTP status is 200
</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
#output {
color: green;
font-size: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>
GeeksforGeeks
</h1>
<label id="File_Path">
<b>Enter File Path: </b>
</label>
<input type="text" id="File_URL">
<button id="Check_File" onclick="checkFileExist()">
click here
</button>
<p id="output"></p>
<script>
var url = document.getElementById("File_URL");
var output = document.getElementById("output");
var http = new XMLHttpRequest();
function checkFileExist() {
if (url.length === 0) {
output.innerHTML = "Please enter File URL";
} else {
http.open('HEAD', url, false);
http.send();
if (http.status === 200) {
output.innerHTML = "File exists";
} else {
output.innerHTML = "File doesn't exists";
}
}
}
</script>
</body>
</html>