Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 275
Vervatovskis Avatar asked Sep 11 '12 08:09

Vervatovskis


People also ask

How do I download a file instead of open in browser?

Click on "Settings" and you'll see a new page pop up in your Chrome browser window. Scroll down to Advanced Settings, click Downloads, and clear your Auto Open options. Next time you download an item, it will be saved instead of opened automatically.

How do I open a download window?

Open Windows Explorer ⊞ Win + E . Your Downloads folder may be listed in the left frame under "Favorites" or "Computer/This PC". Press ⊞ Win + R and type shell:downloads . Press ↵ Enter to open the Downloads folder.


2 Answers

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.

like image 199
Salketer Avatar answered Sep 20 '22 17:09

Salketer


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

like image 35
Manse Avatar answered Sep 18 '22 17:09

Manse