Checking the existence of a file - JS

Asked

Viewed 65 times

0

Good night

I want to know if a particular file in a directory exists or not using pure JS, but none of the Answers on the internet worked.

answers I tried:


the answer 2 returns these errors: erros retornados ao tentar esse metodo


would like to know how I can check whether or not one exists in a directory using only JS.

My code

function abrirPagina(){
    var url = "umcaminhoqualquer/index.html";

    if (validarPagina(url)){
        window.location.href = (url); //redireciona para uma pagina local
    }else{
        alert('pagina ainda não existe');
    }
}

function validarPagina(url){
  //verificar e retornar true caso exista
}

thank you in advance

  • 1

    to check a "folder" on a site has no way... but for files, you can use ajax, and see if the returned code is 404 (not found), look: https://www.w3schools.com/js/js_ajax_intro.asp , in the case of this api, the code will be returned in status...but will only be the definitive when readyState for 4

  • 1

    ah, and it seems that the endemics you were trying were for the computer, summarizing it is impossible, a web page js access the computer file system..., what you can access are just other web pages, and depending on the configuration of the servers, only from the same site

  • 1

    if you want to access/manage PC files with JS recommend Node.js

1 answer

1


In fact it is right both the answer 1 and the answer 2, probably Voce is not running its application on any server, and precisely for this reason Voce has this error, I suggest Voce try to use some server. Example: NODE

Good luck.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.