Home > Web Front-end > JS Tutorial > How to implement downloading in javascript

How to implement downloading in javascript

奋力向前
Release: 2023-01-07 11:47:34
Original
4711 people have browsed it

Javascript method to implement downloading: 1. Use the href attribute of the a tag to add the file URL, with the syntax "Download"; 2. Use URL jumps to download, the syntax is "window.open(file url)".

How to implement downloading in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer

Use JS to achieve downloading File function?

The common method is to create a new download:

1. a tag

##Compressed package download:

<a href="前端框架-Minton.rar">下载</a>
Copy after login

Download other files

 function commDownload1(url, params) {
     url += "?";
    for(let key in params) {
         url += key + "=" + params[key] + "&";
     }
     url = url.substr(0, url.length - 1);
     $("<a href=" + url + " />")[0].click();
 }
Copy after login

2. URL jump

function commDownload2(url, params) {
     url += "?";
     for(let key in params) {
         url += key + "=" + params[key] + "&";
     }
    url = url.substr(0, url.length - 1);
    window.open(url);
Copy after login

Among them, the

a tag has a better experience. The other two will open a new tag and download again. The whole process of closing the new tab feels dizzying, so it is recommended to use the a tag to implement the download function.

Recommended learning:

JS video tutorial

The above is the detailed content of How to implement downloading in javascript. 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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template