使用Javascript扫描本地目录,然后使用“最新"标签更新Anchor标签.文件 [英] Use Javascript to scan a local directory and then update an Anchor tag with the "latest" file

查看:187
本文介绍了使用Javascript扫描本地目录,然后使用“最新"标签更新Anchor标签.文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的HTML页面,该页面基本上会加载一些JavaScript并检查我的方向(../Files) 文件文件夹包含

I'm trying to create a simple HTML page that will basically, load up some javascript, and check my direction (../Files) the Files folder contains

file_001.txt
file_002.txt
file_003.txt

然后,我希望javascript使用最新的文件(file_003.txt),并更新ID为"file_download"的锚标记.

I then want javascript to use the latest one (file_003.txt), and update a anchor tag with the id of "file_download".

任何人都会有一个想法如何做到这一点吗? 原因是,假设我有一个条款和条件PDF文件T& C_001.pdf,并下载了发布新条款和条件的行.因此我们保留了T& C_001.pdf的所有旧记录,并上传了T& C_002.pdf.现在,这将不需要任何HTML知识甚至Java脚本.该站点的所有者只需要添加一个末尾带有002的新文件即可.

Would anyone by any chance have an idea how to do this? The reason is, lets say I have a terms and conditions PDF file that is T&C_001.pdf and download the line a new terms and conditions is released. so we keep the T&C_001.pdf for any old records and we upload T&C_002.pdf. Now this will not need any HTML knowledge or even Javascript. The owner of the site would just need to add a new file with 002 on the end.

推荐答案

尽管不够准确,您可以尝试

though, not fully accurate, you could try

<script type="text/javascript">
function getLatest(location, filename, ext, readyCallback, index) {
    var tgIndex = typeof index === 'undefined' ? 1 : index;
    $.ajax({
        type: 'HEAD',
        url: location + '/' + filename + '-' + index + '.' + ext,
        success: function() {
            getLatest(location, filename, ext, readyCallback, tgIndex + 1);
        },
        error: function() {
            if (tgIndex > 1) {
                readyCallback(location + '/' + filename + '-' + (tgIndex-1) + '.txt');
                return;
            }
            readyCallback();
        }
    });
}
getLatest('linkToSite', 'file', 'txt', function(filename) {
    if (typeof filename === 'undefined') {
        alert('No file found on the location');
    }
    // do something with returned filename
});
</script>

,当上一个索引不是最新索引时,它将尝试检查文件是否存在.如果第一个失败,则指定位置上没有文件

which would try to check if the file is there, when not the previous index was the latest one. If the first one fails, there is no file on the specified location

此回复不添加格式,而是期望文件为: -file-1.txt,file-2.txt,...

This reply doesn't add formatting, and rather expects the files as: - file-1.txt, file-2.txt, ...

此示例假设使用jquery:)

this example assumes jquery :)

可以在这里找到它的小提琴

a fiddle of it you can find here

http://jsfiddle.net/Icepickle/JL5Su/

这篇关于使用Javascript扫描本地目录,然后使用“最新"标签更新Anchor标签.文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆