javascript - Password protected download link - Stack Overflow

I want to make a simple password protected link that enables users (with the correct password) to downl

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>

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>
Share Improve this question edited Jun 22, 2016 at 13:43 István 5,12710 gold badges40 silver badges68 bronze badges asked Jun 22, 2016 at 13:04 Ched JoseChed Jose 1031 gold badge3 silver badges8 bronze badges 5
  • 2 That is nowhere near a sufficient password protection. The file needs to be password protected on the server. As long as I can simply type the file's direct URL into my browser and download it, it is not protected. And both the direct URL and the password required for your little check here are plainly visible in the Javascript itself, which is visible to anyone bothering to look. – deceze Commented Jun 22, 2016 at 13:07
  • I know it is not a good way to protect a link, but I am not looking for something more than this. I just want the password popup box to open. – Ched Jose Commented Jun 22, 2016 at 13:09
  • Is there an error on the browser's JavaScript console? – David Commented Jun 22, 2016 at 13:19
  • I am assuming that because, it is not working. When I test it and click on the link, the password popup box doesn't open. – Ched Jose Commented Jun 22, 2016 at 13:21
  • Have you looked at your browser's Javascript console for errors? Typically you can open it with Ctrl+Alt+C... – deceze Commented Jun 22, 2016 at 13:23
Add a ment  | 

1 Answer 1

Reset to default 3

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()"/>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745467083a4628962.html

相关推荐

  • javascript - Password protected download link - Stack Overflow

    I want to make a simple password protected link that enables users (with the correct password) to downl

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信