menu

Questions & Answers

Browser not loading simple javascript

I am trying to create a simple javascript project but I am facing a very simple issue. My javascript source code is not being loaded from my browser. I have simplified the code for simplicity:

<!DOCTYPE > <!DOCTYPE html>
<html lang="en">
  <head>
    <title>My Testing works</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

  </head>
  <body>
    <script>
      type="text/javascript"
      src="main.js"
    </script>
    <main>
      <h1>My Page works</h1>
    </main>
  </body>
</html>

And my simple javascript:

console.log('javascript source working')
alert("Message")

The Devtools from chrome displays the following

enter image description here

I have enabled javascript on my chrome. What am I doing wrong ?

Answers(2) :

You have a typo:

<!-- no closing bracket before attributes -->
<script
  type="text/javascript"
  src="main.js">
</script>

Furthermore, scripts are usually declared on a single line (open-close). This is convention.

<script type="text/javascript" src="main.js"></script>

Unrelated, but kind of related :-

You may have seen the self-closing tag convention in various elements such as <link /> and <meta /> elements. You cannot do the following, because "both the starting and ending tag[s] are mandatory".

<script type="text/javascript" src="main.js" /> <!-- not possible -->

Although it would be nice if you could…

Your script tag is closing too soon

    <script>
      type="text/javascript"
      src="main.js"
    </script>

needs to be

    <script
      type="text/javascript"
      src="main.js">
    </script>