最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to place Javascript in a separate file from HTML - Stack Overflow

programmeradmin5浏览0评论

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>

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>
Share Improve this question edited Aug 24, 2015 at 20:39 Paul Roub 36.4k27 gold badges86 silver badges95 bronze badges asked Aug 24, 2015 at 20:31 IgorsIgors 931 gold badge1 silver badge6 bronze badges 3
  • 2 get all between <script> tags and make new file from it. Then use <script src="...path to your file"></script> – bksi Commented Aug 24, 2015 at 20:33
  • 1 Don't put the <script> tags in your JavaScript file. – Pointy 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) – bksi Commented Aug 25, 2015 at 1:33
Add a ment  | 

1 Answer 1

Reset to default 10

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>
发布评论

评论列表(0)

  1. 暂无评论