38

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.

Manse
  • 37,765
  • 10
  • 83
  • 108
Vervatovskis
  • 2,277
  • 5
  • 29
  • 46

4 Answers4

63
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.

Salketer
  • 14,263
  • 2
  • 30
  • 58
  • 9
    Sorry if this is a stupid question but where do you put Content-Disposition: attachment; filename="mypdf.pdf";? – Marcel Jul 14 '14 at 18:49
  • 1
    @Marcel this is likely too late but headers must be set at the destination url and cannot be set by Javascript. See: http://stackoverflow.com/a/4326141/810794 – Luke Keller Feb 26 '15 at 14:57
  • @Marcel , You put it in response from server. Here is java code: ```Response.ok(fileContent, "application/octet-stream").header("Content-disposition", "attachment; filename=mypdf.pdf").build(); ``` – vanduc1102 Jul 04 '16 at 03:41
  • 1
    @vanduc1102 thank you for the code. I have had this figured out for a long time. – Marcel Jul 04 '16 at 04:12
  • Super helpful! Thanks! – techcase Mar 04 '22 at 14:34
13

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

Manse
  • 37,765
  • 10
  • 83
  • 108
  • I still have probleme with internet explorer, if i have a .pdf file, it open it and i have un encrypted file on my browser. for chrome and firefox it works very well. – Vervatovskis Sep 11 '12 at 08:40
  • Where are you serving the PDF from ? what is the MIME type being sent with the PDF ? what does IE show ? – Manse Sep 11 '12 at 08:42
  • 1
    Thats what i get in IE when i launch the download ›É"¨36k.yš6¢Š(« ä÷§äš Ôø;}êÌ$‘ÍK„¯4QEóH1Ò‡$Å"ä¶*5ÞÒyŸéQEúŒœR‚¯Zšá8â¤q‘EQQàŽ¦ž¤æšsšQ‘TÆàj˜Þz(¢ŠÕÒîÌR)ÏC]ŕȕ†é^qQ±]..... – Vervatovskis Sep 11 '12 at 08:49
  • I am using a HttpHandler by the way – Vervatovskis Sep 11 '12 at 08:52
8

HTML5 solution with 'download' attribute

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

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

Krad
  • 171
  • 3
  • 7
  • Definitely take a look at this https://caniuse.com/#feat=download before using this solution – ryankdwyer Jan 08 '18 at 21:53
  • The download attribute is not supported in Edge version 12, IE, Safari 10 (and earlier), or Opera version 12 (and earlier – nullify0844 Nov 01 '18 at 00:55
  • Also, since it is a javascript function that is launching the download, I think that creating a link, hiding it and then sending a click event on it is a lot of work for just a one-liner javascript... – Salketer Apr 11 '19 at 08:10
5
<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