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.

4 Answers 4

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

5
  • 9
    Sorry if this is a stupid question but where do you put Content-Disposition: attachment; filename="mypdf.pdf";?
    – Marcel
    Jul 14, 2014 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: stackoverflow.com/a/4326141/810794 Feb 26, 2015 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 4, 2016 at 3:41
  • 1
    @vanduc1102 thank you for the code. I have had this figured out for a long time.
    – Marcel
    Jul 4, 2016 at 4:12
  • Super helpful! Thanks!
    – techcase
    Mar 4, 2022 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

4
  • 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. Sep 11, 2012 at 8: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, 2012 at 8: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±]..... Sep 11, 2012 at 8:49
  • I am using a HttpHandler by the way Sep 11, 2012 at 8:52
8

HTML5 solution with 'download' attribute

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

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

3
  • Definitely take a look at this caniuse.com/#feat=download before using this solution
    – ryankdwyer
    Jan 8, 2018 at 21:53
  • The download attribute is not supported in Edge version 12, IE, Safari 10 (and earlier), or Opera version 12 (and earlier Nov 1, 2018 at 0: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, 2019 at 8: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
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.