Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda