How to download a file via js?

Good day, there is a variable which stores a link to the file:
when you output via the console
console.log(url_download);
the result is the full url in the format: https://test.ru/temp/file.doc
but when you run
location.href = url_download;
the transition occurs on the link https://test.ru/old/url/test.ru/temp/file.doc

What's the problem, what am I doing wrong, please tell me
March 23rd 20 at 19:03
2 answers
March 23rd 20 at 19:05
Solution
And if you do so...?
window.location = url_download;
The result is no different - Kaitlin.Langworth commented on March 23rd 20 at 19:08
And url_download exactly is https:// at the beginning of the line?
And then JS acts as if it there there.
Make console.log(url_download.substr(0, 8));to check. - arturo_Emmerich commented on March 23rd 20 at 19:11
March 23rd 20 at 19:07
what about this

var download = document.createElement("a");
download.href = "https://test.ru/old/url/test.ru/temp/file.doc" ;
download.download = "file.doc";
download.style.display = "none";
download.id = "download";
document.body.appendChild(download);
document.getElementById("download").click();
document.body.removeChild(download);
download = undefined;
and why
document.body.appendChild(download);
document.getElementById("download").click();
document.body.removeChild(download);

?

In General, why not simply
let link = document.createElement('a');
 link.href = response;
 link.download = name;
 link.click();

, for example? - orville.Daniel53 commented on March 23rd 20 at 19:10

Find more questions by tags JavaScript