Home >Web Front-end >JS Tutorial >Example of how JavaScript implements browser downloading files

Example of how JavaScript implements browser downloading files

黄舟
黄舟Original
2017-08-10 10:49:441877browse

This article uses a simple code to introduce to you the method of JS to implement browser downloading files. Friends who need it can refer to it

No more nonsense, I will directly post the code for you, the specific code As shown below:


function download(src) {
    var $a = document.createElement('a');
    $a.setAttribute("href", src);
    $a.setAttribute("download", "");
    var evObj = document.createEvent('MouseEvents');
    evObj.initMouseEvent( 'click', true, true, window, 0, 0, 0, 0, 0, false, false, true, false, 0, null);
    $a.dispatchEvent(evObj);
  };

This method is found from the Internet and can be called directly. If you use the direct path, you can use

Summarize

The above is the detailed content of Example of how JavaScript implements browser downloading files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn