Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
My jquery code is not running in the proper order and I can't tell if a file exists or not

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Update: I just figured it out and it works now. Thank you so much. Here is the code I'm using now from another question which didn't work when I tried it earlier, but it is all good now:

<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)
                .done(function() {
                alert('File exists!');
                }).fail(function() { 
                  alert('File does not exist')
                });         
        }
    });
});
</script>
</head>
<body>
<span id="myInput" role="textbox" contenteditable></span>
</body>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!