Is it possible to retrieve the last modified date of a file using Javascript?

前端 未结 5 1854
慢半拍i
慢半拍i 2020-12-08 16:10

I have a set of links on a web page that link to PDF forms and .doc forms. These files are not stored in a database, simply stored as they are, locally on the server. Is i

5条回答
  •  心在旅途
    2020-12-08 17:08

    If it's on the same server as your calling function you can use XMLHttpRequest-

    This example is not asynchronous, but you can make it so if you wish.

    function fetchHeader(url, wch) {
        try {
            var req=new XMLHttpRequest();
            req.open("HEAD", url, false);
            req.send(null);
            if(req.status== 200){
                return req.getResponseHeader(wch);
            }
            else return false;
        } catch(er) {
            return er.message;
        }
    }
    
    alert(fetchHeader(location.href,'Last-Modified'));
    

提交回复
热议问题