admin管理员组

文章数量:1312916

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.

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.

Share Improve this question edited Apr 7, 2019 at 19:51 Christian Potts asked Apr 7, 2019 at 19:45 Christian PottsChristian Potts 572 silver badges8 bronze badges 3
  • Check your network tab - sounds like the file isn't being linked properly – CertainPerformance Commented Apr 7, 2019 at 19:46
  • 1 See MDN. – N'Bayramberdiyev Commented 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 Commented Apr 7, 2019 at 19:58
Add a ment  | 

3 Answers 3

Reset to default 5

Add like following not with link.

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

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.

You're using the incorrect link for JavaScript.

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

本文标签: linking JavaScript to HTML fileStack Overflow