I have a use case where I have a URL in a text component that takes the user to an outside webpage, however sometimes this URL gives a 404 error. I am wondering if there is a way inside Retool to be able to output a true if the webpage exists or a false if the 404 error is produced? The purpose of this is that if a 404 error exists on the outside webpage, then the user see's a red checkmark or some attribute so they know what they are look for doesn't exist. I have tried using the below with my URL as the variable but Retool outputs a null value whether it exists or not. Is what I am describing possible in Retool? Thanks
var url = window.location.href;
function UrlExists(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
if (http.status != 404)
return true;
else
return false;
}
You can add the JS under the ... menu
and then call the function in a JS query BUT it seems that there may be a limitation in Retool in doing so.... I do get an error but haven't been able to dig into it . Perhaps someone else has done this before....I have done this in other apps but not in Retool.....a long long time ago
@Tess The code you provided does work for the link I provided as I get a success response, although i get success despite getting a 404 page error in github. ideally the goal is if the user clicks the link and the page produces a 404- page not found like in the screenshot, they get an error else success. Maybe this is a github thing?
These are the two main approaches that come to mind as far as how to implement this in Retool, but I think there's possibly some urls that wouldn't be compatible with either approach (for example, maybe if there's some redirect behavior to account for?)