-1

I use the syntax below to download the file described in it, but it was opening in another page instead of downloading it. The syntax is as follow;

function passwd() {
  var password = prompt('Enter the pin to download the file: ');

  if (password.toLowerCase() == "1472") {
    window.open("folder/file")

  } else {
    alert("incorrect password!! please try again");
  }
}
<input type="button" value="download" onClick="passwd()" />
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • 2
    You understand that it's now actually protected because anyone can see the right password, right? – Mosh Feu Nov 03 '20 at 15:13
  • 2
    Does this answer your question? [Download File Using Javascript/jQuery](https://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery) specifically - https://stackoverflow.com/a/9834261/863110 – Mosh Feu Nov 03 '20 at 15:13
  • 1
    ***Never*** validate passwords on the client side. Always validate it on the server. – shreyasm-dev Nov 03 '20 at 15:13

1 Answers1

0

There are lots of issues in your code as mentioned in the comments before. However, as an answer to your question, if you want to open the window in the same tab, you need to send "_self" parameter. Like this:

 window.open("folder/file", "_self")
Akif
  • 7,098
  • 7
  • 27
  • 53