Javascript not working?

Javascript not working! I tried the following:

  • Writing <!DOCTYPE html><html><script>...</script></html> in a .html file
  • Writing <!DOCTYPE html><script>...</script> in a .html file
  • Writing ... in a .js file
    I checked the languages, but all it says is:
100% html

Who can help me?

Hi. đź‘‹

There are several ways to include JavaScript in an HTML file, the most used are internal or external, both internal and external are usually placed either in the <head> or the last thing before closing the </body>.

<!DOCTYPE html>
<html>
   <head>
      <title>Your title</title>
      <script type="text/javascript">
            let variable = 'I am internal'
      </script>
      <script src="./js/external-01.js" type="text/javascript">
   </head>
   <body>
      ...content

      <script src="./js/external-02.js" type="text/javascript">
      <script type="text/javascript">
            let variable2 = 'I am also internal'
      </script>
   </body>
</html>

Hope that helps your journey into the land of code. :+1:

My answer: This won’t work.
Proof: It says 100% HTML.
Evidence: This :angry:

The language guess on your repository page says precisely nothing about whether the script will work. :slightly_smiling_face:

To elaborate a bit more: Github uses Linguist to guess the language of your files. Those guesses are strictly per file, an HTML file that includes a few scripts will show up as HTML. If you keep your scripts in separate files and include those from your HTML file, the separate script files should show up as Javascript.

Could you explain it clearer?

What you have in your .html files aren’t 100% valid HTML structure, it’s missing the <head> and <body> tags, GitHub doesn’t check for valid structure, it guesses.

What I posted above is a valid HTML structure, the content that you display on your page should be inside the <body> tag, that’s the body of your page so-to-speak, inside the <head> is your metadata, like your page title -> <title>My HTML page</title>, links to your stylesheets <link rel="stylesheet" href="css/my-css-file.css"> etc.

The <html> tags wrap around those tags, making it a valid tree structure, the <DOCTYPE html> is there for browsers and IDE’s to interpret what type of file this is and what standard it should use when rendering the page, <DOCTYPE html> is the document type for HTML5, aka HTML living standard.

Hope that explains it. :slightly_smiling_face:

No, not really.
Here is the code:

<!DOCTYPE html>
<html>
<head>
<title>
Password Protected
</title>
<script type="text/javascript">
alert("Who are you?");
alert("Sign in information:");
var password = prompt("Password?");
if (password) == ("HIDDEN123") {
alert("Access granted");
}else{
history.back();
}
</script>
</head>
<body>
</body>
</html>

That code is the one I used.

If you’re using Chrome or Firefox you should have access to a developer console, which can be accessed in many ways, but usually, you can right-click a page and choose the inspect option, or the equivalent in your language, if you get that open and click the console, your see the following error.

Uncaught SyntaxError: Unexpected token '==' password_protected.html:11 

Which means there’s a syntax error in your file on line 11, in this case, that’s this line

if (password) == ("HIDDEN123") {

A syntax error will error out your compiler meaning whatever’s in there won’t run, change the line to if (password == "HIDDEN123") { and it will work. :slightly_smiling_face:

2 Likes