How to detect support for the HTML5 “download” attribute?

前端 未结 2 1711
后悔当初
后悔当初 2020-11-29 06:39

One of the new features implemented in HTML5 is the download attribute for anchor tags. The benefit of this attribute is that it gives users the means to downlo

2条回答
  •  一整个雨季
    2020-11-29 07:06

    A single-line if condition to keep things simplified:

    if (document.createElement('a').download==undefined && e.target.hasAttribute('download'))
    {
     e.preventDefault();
     console.log('Error: this is a download link, please right-click to save the file.');
    }
    

    Support for the download attribute is spotty (Chrome 14+, Firefox 20+, IE13+, Safari 10+ and no support in (real) Opera. The script above will not interfere with supported browsers.

提交回复
热议问题