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

Example of how JavaScript implements browser downloading files

黄舟
Release: 2017-08-10 10:49:44
Original
1877 people have browsed it

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);
  };
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
Apakah pengumpulan sampah JavaScript?
From 1970-01-01 08:00:00
0
0
0
Apakah fungsi cangkuk JavaScript?
From 1970-01-01 08:00:00
0
0
0
JavaScript 후크 기능이란 무엇입니까?
From 1970-01-01 08:00:00
0
0
0
Was sind JavaScript-Hook-Funktionen?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template