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.
JavaScript
x
18
18
1
<script type="text/javascript">
2
function isValid(){
3
var password = document.getElementById('password').value;
4
if (password == "123")
5
top.location.href="./file.pdf";
6
else
7
{window.location.reload();}
8
}
9
</script>
10
11
<div class="field_pw">
12
<form name="PasswordField" action="">
13
Password:
14
<input type="password" id="password" name="password" class="noborder" style="font-size: 25px">
15
<input type="button" class="button_pw" value="download" onclick="isValid();">
16
</form>
17
</div>
18
Advertisement
Answer
your pw.js file in js folder:
JavaScript
1
8
1
function isValid() {
2
var password = document.getElementById('password').value;
3
if (password == "123")
4
top.location.href="./file.pdf";
5
else
6
window.location.reload();
7
}
8
your html file:
JavaScript
1
9
1
<script type="text/javascript" src="js/pw.js"></script>
2
<div class="field_pw">
3
<form name="PasswordField" action="">
4
Password:
5
<input type="password" id="password" name="password" class="noborder" style="font-size: 25px">
6
<input type="button" class="button_pw" value="download" onclick="isValid();">
7
</form>
8
</div>
9