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
Group Array of Object Nesting Some of the Keys With Specific Names
Static Variables in JavaScript
JavaScript Call() & Apply() VS Bind()
How to Post a File from a Form With Axios
Mydiv.Style.Display Returns Blank When Set in Master Stylesheet
Change Background Image in Body
How to Get a Leaflet Map Canvas to Have a 100% Height
React Native Use Variable for Image File
How to Bind to List of Checkbox Values with Angularjs
How to Set a Cookie for Another Domain
Use of .Apply() With 'New' Operator. Is This Possible
How to List the Properties of a JavaScript Object
Iterating Over Result of Getelementsbyclassname Using Array.Foreach
Get Computed Font-Family in JavaScript
Google Seo and Hidden Elements
Convert Dropdown to Selection Boxes with Color and Trigger Drop Down Action