简体   繁体   中英

Launch download in the same tab without opening new tab or window in Javascript

I am using this javascript function to launch download

function startDownload(url) {
   window.open(url, 'Download');
}

It works, but i want to block any new tab or new window launch, thanks.

function startDownload(url) {

    window.location.href = url;
}

This will start the download in the same page, exactly like when you click a link without any target other than _self .

To force the download of a file, make sure you send the right headers with it:

Content-Disposition: attachment; filename="mypdf.pdf";

This will make sure that the file is not displayed in the browser instead of being downloaded. Replace the filename part with the filename you want as default on the save as dialog.

window.open will open a new window \\ tab (depending on user preferences) ... to just download the file use

window.location.href = url;

You can use this if the url returns a downloadable file rather than a web page

HTML5 solution with 'download' attribute

<a href="/images/myw3schoolsimage.jpg" download>

https://www.w3schools.com/tags/att_a_download.asp

<a target="_parent" href="link"></a>
  • _blank - URL is loaded into a new window. This is default
  • _parent - URL is loaded into the parent frame
  • _self - URL replaces the current page
  • _top - URL replaces any framesets that may be loaded name - The name of the window

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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