I am writing a local html family tree. Since the site is supposed to contain many personal pages that will take me a long to write, I want my browser to display in grey the links to the pages that have still not been created (everything here is on the server side in the project directory). It seemed it was only a matter of checking if a file existed and changing the colour of the link with javascript, but it turned out to be much more complicated than I thought. I read several solutions in google to check if a file exists, and all of them failed. One of them is the following:
function LinkCheck(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status !== 404;
}
This failed. Then I tried to debug, and change the function to:
function LinkCheck(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
//return http.status !== 404;
return false;
}
This should have worked unconditionally since the function returns "false" in any case. But this one failed too. Then I disabled the "send" part:
function LinkCheck(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
//http.send();
//return http.status !== 404;
return false;
}
An this worked! So, the problem is the function http.send() that causes a failure. Any insight?
Note: I tried with Firefox and Chrome, with the same results.
EDIT 1: The JavaScript code that actually uses this function is:
if (LinkCheck("Abraham_page.html")){} else {
document.getElementById("Abraham_link_id").style.color = "gray";
}
Where "Abraham_page.html" may be located inside the same directory as the page containing the calling function (but actually don't exist).
Edit 2: I'm not use to the debugger tool, but it seems the relevant message is:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///C:/Tex-Dir/Tex_Work/User/html/genealogy/Abraham_page.html. (Reason: CORS request not http).