简体   繁体   中英

External JS not linking to HTML

I have a very simple directory structure with an index.html file and a main.js file at the same level and no other files/folders. But I'm unable to link this js file in my index.html.

I have posted the contents of both files below, and the expected behaviour is to have a simple alert pop-up.

Here is my HTML File:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="css/main.css" />
        <script type="text/javascript" scr="main.js"></script>
        <title>External JS Issue</title>
    </head>
    <body>
        Some Content Here.
    </body>
</html>


Here is my JS File:
alert("If you can read this, it worked;");


However, if I include this JS line of code and put it directly inside my index.html , it works fine.
Here are the screenshots:

index.html:
索引.html

main.js:
main.js

Browser Screenshot with External JS(NOT Working):
带有外部 JS 的浏览器屏幕截图(不工作)


index.html with inline JS:
index.html 与内联 JS

Browser Screenshot with Inline JS(Working):
带有内联 JS 的浏览器屏幕截图(工作中)

You have a typo, the attribute of the <script> tag should be src , not scr .

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