Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda