简体   繁体   English

如何下载使用javascript动态生成的文件列表?

[英]How can I download a list of files dynamically generated with javascript?

I'm trying to create and loop through a list of links that point towards a dynamically generated XML file. 我正在尝试创建并循环访问指向动态生成的XML文件的链接列表。

Here is the code I'm using . 这是我正在使用的代码。 . . I've changed the url: 我已经更改了网址:

    function scanSystem() {
    // Starting value for scans
        var xMin = '0';
        var yMin = '0';
    // Temporary - The highest coordinates that will be scanned
        var coordMax = '19';

        for (var x = xMin; x <= coordMax; x++) {
            for (var y = yMin; y <= coordMax; y++) {
                var url = "http://www.xmlurl.com/members/scanners/list.php?cockpit&xml&x=" + x + "&y="+ y;
                var link = "<a href='" + url + "'>(" + x + "," + y + ")</a><br>";
                $("#links_container").append(link);
                document.getElementById('iframe').src = url;
            }
        }
    }

The links generate and fill the div (which is more of a debug function at the moment) but only the last link is actually downloaded through the iframe. 这些链接会生成并填充div(目前更多是调试功能),但实际上只有最后一个链接是通过iframe下载的。

Any ideas how to make it start the download of the other files as well? 有什么想法可以使它也开始下载其他文件吗?

Rather than opening the URLs in an iframe, just open them with window.open . 与其在iframe中打开URL,不如通过window.open打开它们。 Most browsers will immediately close the new window if it only downloads a file. 如果仅下载文件,大多数浏览器将立即关闭新窗口。

A way to do it is to dynamically create a hidden iframe for each link instead of opening new windows (which will be annoying). 一种方法是为每个链接动态创建一个隐藏的iframe ,而不是打开新窗口(这会很烦人)。 Each iframe will be loading each file individually. 每个iframe将分别加载每个文件。

Taking advantage that you are using jQuery already, here's sample code: 利用您已经在使用jQuery的优势,这里是示例代码:

function scanSystem() {
  var x
    , y
    , coordMax = 19
    , url
    , linksContainer = $("#links_container")
    ;

  for (x = 0; x <= coordMax; x++) {
    for (y = 0; y <= coordMax; y++) {

      url = 'http://www.xmlurl.com/members/scanners/list.php?cockpit&xml&x=' + x + '&y=' + y;
      $('<a/>',{
          href : url
        })
        .text(x+','+y)
        .appendTo(linksContainer);

      $('<iframe/>',{
          src : url
        })
        .appendTo('body');
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加到 iOS 主屏幕时,如何在 PWA 上下载动态生成的 PDF 文件? - How can I download dynamically generated PDF files on PWA when added to home screen on iOS? jQuery / Javascript:如何获取动态生成的控件的ClientID - Jquery/Javascript: How can I get ClientID for dynamically generated controls 如何删除 JavaScript 中动态生成的事件处理程序 - How can I remove dynamically generated event handlers in JavaScript 如何在运行时动态更改javascript生成的hss的css? - How can I dynamically change css for html that is generated by javascript at runtime? 如何在JavaScript生成的输出中包含PHP文件的内容? - How can I include the content of PHP files in output generated by JavaScript? 如何下载使用javascript元素的一组文件? - How can I download a group of files with a element use javascript? 如何使用 Javascript/jQuery 在动态生成的 forms 中动态输入字段 select? - How can I dynamically select input field in dynamically generated forms with Javascript/jQuery? 下载由Javascript生成的动态创建的CSS文件 - Download a dynamically created CSS file generated by Javascript 如何动态加载和执行目录中的所有JavaScript文件的? - How can I dynamically load and execute all of the Javascript files in a directory? 可以将Nodejs模块下载为javascript文件吗? - can I download Nodejs modules as javascript files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM