简体   繁体   English

如何以编程方式单击IE中的链接?

[英]How to programmatically click a link in IE?

I'm having a problem specific to IE with this function: 我有一个特定于IE的问题与此功能:

function downloadFileFromUserControl(filename) {
    var name = filename.split("/");
    var fName = name[name.length - 1];
    console.log("IE speaking, I'm just going to take a little nap during this request");
    var link = document.createElement('a');
    link.download = fName;
    link.href = filename;
    link.click();
    console.log("Oh, I'm late to the party?? Back to sleep I guess");
}

In Chrome this function works perfectly, I use this to download PDF files from hyperlinks. 在Chrome中,此功能完美运行,我使用此功能从超链接下载PDF文件。 No errors, it just will post to the console. 没有错误,它只会发布到控制台。

Is there some additional code IE needs to make this work? 是否有一些额外的代码IE需要使这项工作? Thanks 谢谢

After the element is created, use the element.appendChild() or element.insertBefore() method to insert it to the document . 创建元素后,使用element.appendChild()element.insertBefore()方法将其插入到document

It is how it works, refer w3c reference 它是如何工作的,参考w3c参考

IE does NOT support the "download" attribute only newer browsers ie. IE不支持“下载”属性只有较新的浏览器即。 Edge, you need to be careful with using new HTML5 functionality in old browsers, most of the time you need to add some sort of "fallback" for this particular case I'd suggest: Edge,你需要小心在旧浏览器中使用新的HTML5功能,大多数时候你需要为这个特殊情况添加某种“后备”我建议:

    if (typeof link.download !== typeof undefined && link.download !== false) {
       var message = document.getElementById('Some <Span> ID Here').innerHTML('Right-click and select "Download Linked File"');
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM