7

I have this code placed in index.html, all together - Javascript and HTML. what I want is to place the JS into another, separate file called "pw.js".

I have created a blank JS file located in folder "js". I have added it to (head) as well like this <script src="js/pw.js"></script>

This is what I have now in index.html, how to place the JS separatly? Please be specific in answers, I'm a beginner.

<script type="text/javascript">
  function isValid(){
    var password = document.getElementById('password').value;
    if (password == "123")
      top.location.href="./file.pdf";
    else
      {window.location.reload();}
  }
</script>

<div class="field_pw">
  <form name="PasswordField" action="">
    Password:
    <input type="password" id="password" name="password" class="noborder" style="font-size: 25px">
    <input type="button" class="button_pw" value="download" onclick="isValid();">
  </form>
</div>
3
  • 2
    get all between <script> tags and make new file from it. Then use <script src="...path to your file"></script> Commented Aug 24, 2015 at 20:33
  • 1
    Don't put the <script> tags in your JavaScript file. Commented Aug 24, 2015 at 20:34
  • Note that it is better to make ajax call to secure your password via server side script (db or whatever) Commented Aug 25, 2015 at 1:33

1 Answer 1

11

your pw.js file in js folder:

function isValid() {
    var password = document.getElementById('password').value;
    if (password == "123")
        top.location.href="./file.pdf";
    else 
        window.location.reload();
}

your html file:

<script type="text/javascript" src="js/pw.js"></script>
<div class="field_pw">
    <form name="PasswordField" action="">
        Password:
        <input type="password" id="password" name="password" class="noborder" style="font-size: 25px">
        <input type="button" class="button_pw" value="download" onclick="isValid();">
    </form>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.