4

I am at my wits end with linking a javaScript file to my main.html, they are both in the same folder.

I have viewed multiple Stack overflow topics regarding this question, and for some reason none of the solutions solve my answer.

My current code is what I have below:

[main.html]

 <!DOCTYPE html>
 <head>
   <link rel="javascript" href="jsfile.js">

   <title>JavaScript</title>
 </head>
   <body>
      <div class="Header">
         <h1>JS Basics</h1>
      </div>
   </body>

[jsfile.js]

document.write("Hello World");

This code does not display the "Hello World" message, however if i insert the code directly into the HTML file it does.

I appreciate any and all help on this matter.

3
  • Check your network tab - sounds like the file isn't being linked properly Apr 7, 2019 at 19:46
  • 1
    See MDN. Apr 7, 2019 at 19:57
  • href is not the way to include js. You should use src instead, and it should be in a script element, not link. That approach you used is for styling.
    – Mihai
    Apr 7, 2019 at 19:58

3 Answers 3

5

Add like following not with link.

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

Just type:

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

Most importantly add this line to the bottom of your body tag to avoid your browsing requesting JavaScript to be loaded before displaying your HTML.

enter image description here

0

You're using the incorrect link for JavaScript.

Try using this instead <script src="jsfile.js"></script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.