I wrote this code to check if a file exists and if it does, do one thing and if it doesn't, do another by creating a variable.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script> $(document).ready(function(){
$("#myInput").keyup(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
var exists = false;
var filename = "folder/document.html";
$.get(filename, function() { //This code only runs if a certain file exists
exists = true;
alert('should be displayed first');
});
if(exists == true) {
alert('This file exists!');
} else {
alert('This file does not exist!');
}
}
});
});
</script>
</head>
<body>
<span id="myInput" role="textbox" contenteditable></span>
</body>
The problem with it is that for some reason, $.get runs after everything else, no matter what I do. Why is this happening and how do I solve it. I'm fine with a totally new method of checking if a file exists, but this is the best I could find. The $.get releases a 404 error if it does not find a file, meaning that the code inside of it only runs if it finds a file, which is why I had it set a variable inside of there and had the if statement be reliant on the variable.
PS: the code only works if it is uploaded to the internet, not as a file on a computer, which is really annoying.