Search code examples
htmlsrcscript-tag

Html script doesn't work anymore after moving into source file - why so? how to fix?


I got an html file that looks somewhat like this:

<html>
<head>
  <script type="text/javascript" src="lang-parser.js"></script>
  <script id="code" type="text/some-scripting-language">
  //some code here in some language that is not js
  </script>
  <script type="text/javascript" src="sketch.js"></script>
</head>
<body>
  <div id="canvas"></div>
</body>
</html>

Everything works and seems fine. Now I take the code in the middle script and move it into another file, and change the html code for that to:

  <script id="code" src="code.cdy" type="text/some-scripting-language"></script>

and it stops working, without any explicit errors, as if the script didn't load in time or something.

My question is: what can be the reason for this behavior and how can I change it?

Notes:

  1. Even if I write a wrong name of the file the console doesn't inform me about non-existent file.
  2. If I additionally write that the file is of type text/javascript (which it is not), when the console finally informs me the file doesn't exist.
  3. IMHO: It looks like the language parser gets downloaded asynchronously, and while it is in the process the parser reads the next tag, sees unknown type of language there and just ignores the entire thing... But that doesn't explain why without the source the code works.

Solution

  • Because the lang parser js does not see the contents of your script because the element with id 'code' is empty, and the src tag does not mean anything to it