How to Check If File Exists in Jquery or Pure JavaScript

How do I check if file exists in jQuery or pure JavaScript?

With jQuery:

$.ajax({
url:'http://www.example.com/somefile.ext',
type:'HEAD',
error: function()
{
//file not exists
},
success: function()
{
//file exists
}
});

EDIT:

Here is the code for checking 404 status, without using jQuery

function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}

Small changes and it could check for status HTTP status code 200 (success), instead.

EDIT 2: Since sync XMLHttpRequest is deprecated, you can add a utility method like this to do it async:

function executeIfFileExist(src, callback) {
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (this.readyState === this.DONE) {
callback()
}
}
xhr.open('HEAD', src)
}

Test if a file exists with javascript

You can use $.ajax

$.ajax({
url: 'example.com/abc.html', //or your url
success: function(data){
alert('exists');
},
error: function(data){
alert('does not exist');
},
})

In HTML5 app, how to check if a file exists with javascript?

Try to load it and listen to onerror event

var myImage = new Image();
myImage.src = 'images/icon.png';
myImage.onerror = function(){
alert('not found');
}

Check if the first css file of a link tag exists in pure javascript

Content page:

<link href="/v1/css/main.min.1614781878390.css" rel="stylesheet" />

New code:

// refresh if the client does not find the first css file
var qs = document.querySelector("link[rel='stylesheet']");
var MrChecker = new XMLHttpRequest(),
CheckThisUrl = qs.getAttribute('href');

MrChecker.open('get', CheckThisUrl, true);
MrChecker.onreadystatechange = checkReadyState;

function checkReadyState() {
if (MrChecker.readyState === 4) {
if ((MrChecker.status == 200) || (MrChecker.status == 0)) {
//alert(CheckThisUrl + ' page is exixts');
}
else {
//alert(CheckThisUrl + ' not exists');
setTimeout(function(){document.location.reload();}, 2000);
return;
}
}
}

MrChecker.send(null);

I got there, I was inspired by https://jsfiddle.net/pu3antasyah/v95Loyvu/

Check if a file exists with JQuery on local server (in a loop)

Synchronous approach:

function lookForFiles(helper, imageArray){
$.ajax({
url: '/paper' + helper + '/index.png',
type: 'HEAD',
statusCode: {
404: function () {
console.log('nope');
return imageArray;
}
},
success: function () {
console.log('paper' + helper + '/index.png');
imageArray[helper - 1] = 'paper' + helper + '/index.png';
helper++;
lookForFiles(helper,imageArray);
}
});
}

var helper = 1;
var imageArray = new Array;
var images = lookForFiles(helper,imageArray);

javascript check if file exists

You can include a function like this:

function fileExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}

And then modify your code like so:

var adm1_path = "../topo/"+d.id+"_adm1.json"
if (!fileExists(adm1_path)) {
alert('We couldn't find that country!') //or some other suitable error/display mechanism
return;
}

d3.json(adm1_path, function(error, topology) {


Related Topics



Leave a reply



Submit