Ched Jose Ched Jose - 5 months ago 65
HTML Question

Password protected download link

I want to make a simple password protected link that enables users (with the correct password) to download a zip file. The link, as in the code below, is "folder/history.zip". The link is a simple text ("Open"), not a button. I don't have any experience with javascript. The problem is that the password protection does not work when I tried. I just want to know how can I edit the code below to make it work?.. I don't have any experience with javascript so I appreciate any help!

html:

<a href="folder/history.zip">open</a>


Javascript:

<SCRIPT type="text/javascript">
function passWord() {
var testV = 1;
var pass1 = prompt('Please Enter Your Password',' ');
while (testV < 3) {
if (!pass1)
history.go(-1);
if (pass1.toLowerCase() == "teacher") {
alert('You Got it Right!');
window.open('folder/history.zip');
break;
}
testV+=1;
var pass1 = prompt('Access Denied - Password Incorrect, Please Try Again.','Password');
}
if (pass1.toLowerCase()!="password" & testV ==3)
history.go(-1);
return " ";
}
</SCRIPT>
<CENTER>
<FORM>
<input type="text" value="Enter Protected Area" onClick="passWord()">
</FORM>
</CENTER>

Answer

try the below code,

JS:

 function passwd(){

  var password = prompt('Enter the password to download the file:');
  if(password.toLowerCase() == "teacher"){
    window.open("folder/history.zip")    
  }else{
    alert("incorrect password!! please try again");
  }
}

HTML

<input type="button" value="download zip file" onClick="passwd()"/>
Comments