js file exist code example
Example 1: javascript file exists check
function doesFileExist(urlToFile) {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', urlToFile, false);
xhr.send();
return xhr.status !== 404;
}
Example 2: js check file exist
import fs from 'fs';
const path = './file.txt';
try {
if (fs.existsSync(path)) {
}
} catch(err) {
console.error(err);
}
Example 3: check if file exists javascript
function executeIfFileExist(src, callback) {
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (this.readyState === this.DONE) {
callback()
}
}
xhr.open('HEAD', src)
}