Check if a file exists locally using JavaScript only

后端 未结 10 1708
盖世英雄少女心
盖世英雄少女心 2020-12-03 14:01

I want to check if a file exists locally, where the HTML file is located. It has to be JavaScript. JavaScript will never be disabled. jQuery is not good but can do.

10条回答
  •  半阙折子戏
    2020-12-03 14:29

    You can use this

    function LinkCheck(url)
    {
        var http = new XMLHttpRequest();
        http.open('HEAD', url, false);
        http.send();
        return http.status!=404;
    }
    

提交回复
热议问题