the7pm
the7pm

Reputation: 5

How do I connect the javascript file to the html?

Hello I am making a login website and i just want to have the javascript with the password and username in an different file just to make it a little bit harder to find(its just a school project). But my problem is that it wont verify and redirect to my Main.html code after you click the login button.

As you can see in the code i have tried to connect it with the code.

<script src="Login.js"></script>

Here is the html code:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
      <meta charset="utf-8">
      <title>kjedelig AF</title>
      <link rel="stylesheet" href="Login.css">
  </head>
  <body oncontextmenu="return false;">

<form class="box" action="Login.html" method="post" name="login">
  <div class="login">
    <h1>Kjedelig AF</h1>
    <input type="text" name="usrname" placeholder="Username">
    <input type="password" name="pswrd" placeholder="Password">
    <input type="submit" onclick="return check(this.form)" value="Login">
  </div>
</form>
<script src="Login.js"></script>
  </body>
</html>

And here is my javascript code witch i have problems connecting to the html code:

<script language="javascript">
  function check(form){
    if(form.usrname.value == "dd" && form.pswrd.value == "dd")  {
    window.location.href= "Main.html";
    return false;
    }
    else
    {
    alert("Iks dette er kjedelig AF :)")
    }
    return true;
    }
</script>

Upvotes: 0

Views: 59

Answers (2)

user10336445
user10336445

Reputation:

Two reasons:

  1. The script link to the JS file should be anywhere within the <head></head> tags
  2. The JS file shouldn't have the <script language="javascript"></script>, all that the JS file needs to have is:
function check(form){
    if(form.usrname.value == "dd" && form.pswrd.value == "dd")  {
    window.location.href= "Main.html";
    return false;
    }
    else
    {
    alert("Iks dette er kjedelig AF :)")
    }
    return true;
}

Hope this helps.

Upvotes: 1

Shriekin Ninja
Shriekin Ninja

Reputation: 96

First of all, include your JS file at the <head>. there is no good reason not to.

To fix your issue remove <script language="javascript"></script> from your .js file

you should use <script language="javascript"></script> when inside .html file not .js

Upvotes: 1

Related Questions