QUESTION:
How to check if a url is valid and actually loads a page ?
With my current code, only the status code is checked, which means that a url like http://fsd.com/ will be considered as valid although it does not load anything.
How can I check that the url actually points to a website that can be loaded ?
CODE:
$.ajax({
url: link,
dataType: 'jsonp',
statusCode: {
200: function() {
console.log( "status code 200 returned");
validURL = true;
},
404: function() {
console.log( "status code 404 returned");
validURL = false;
}
},
error:function(){
console.log("Error");
}
});
EDIT: By valid, I mean that the page is at last partially loaded (as in at least the html & css are loaded) instead of loading forever or somehow failing without the status code being 404.
EDIT2: http://fsd.com actually returns a 404 now as it should...
EDIT3: Another example: https://dsd.com loads an empty page (status code 200) and http://dsd.com actually loads a page with content (status code 200). On my Node.js backend, the npm package "url-exists" indicates that https://dsd.com is invalid, while my frontend with the code shown in my question indicates it is a valid url. This is what the package code looks like: https://github.com/boblauer/url-exists/blob/master/index.js but I wanted to know what would be the best way according to SO users.
EDIT4:
Sadly, the request provided by Addis is apparently blocked by CORS which blocks the execution of the rest of my code while my original request did not.
$.ajax({
type: "HEAD",
url: link,
dataType: 'jsonp',
}).done(function(message,text,response){
const size = response.getResponseHeader('Content-Length');
const status = response.status;
console.log("SIZE: "+size);
console.log("STATUS: "+status);
if(size > 0 && status == "200") {
$("#submitErrorMessage").css("display","none");
$('#directoryForm').submit();
}
else {
$("#submitErrorMessage").css("display","block");
$("#submitLoading").css("display","none");
}
});
EDIT 5:
To be more precise, both requests trigger a warning message in the browser console indicating that the response has been blocked because of CORS but my original code is actually executed in its entirety while the the other request doesn't get to the console.log().
EDIT 6:
$.ajax({
async: true,
url: link,
dataType: 'jsonp',
success: function( data, status, jqxhr ){
console.log( "Response data received: ", data );
console.log("Response data length: ", data.length);
console.log("Response status code: ", status);
if (status == "200" && data.length > 0) {
$("#submitErrorMessage").css("display","none");
$('#directoryForm').submit();
}
else {
$("#submitErrorMessage").css("display","block");
$("#submitLoading").css("display","none");
}
},
error:function(jqXHR, textStatus, errorThrown){
console.log("Error: ", errorThrown);
}
});
Error:
Error: Error: jQuery34108117853955031047_1582059896271 was not called
at Function.error (jquery.js:2)
at e.converters.script json (jquery.js:2)
at jquery.js:2
at l (jquery.js:2)
at HTMLScriptElement.i (jquery.js:2)
at HTMLScriptElement.dispatch (jquery.js:2)
at HTMLScriptElement.v.handle (jquery.js:2)