简体   繁体   中英

linking JavaScript to HTML file

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.

添加喜欢以下而不是链接。

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM