简体   繁体   English

在 HTML 中本地采购 Javascript

[英]Locally sourcing Javascript in HTML

Extreme beginner here guys so please explain as easily as possible.这里的极端初学者,所以请尽可能简单地解释。
I've read multiple variations of this and still am unable to figure it out, any help is greatly appreciated.我已经阅读了这个的多种变体,但仍然无法弄清楚,非常感谢任何帮助。

What I am wanting is a local environment to be able to learn HTML & javascript, but cannot get the script source inside HTML to correctly reference the .js file sitting in the exact same folder as the .html file.我想要的是一个能够学习 HTML 和 javascript 的本地环境,但无法在 HTML 中获取脚本源以正确引用与 .html 文件位于完全相同文件夹中的 .js 文件。
I am testing in a Chrome browser just referencing the .html file on my local machine via file:///C:/JavaScript/Index.html.我正在 Chrome 浏览器中进行测试,只是通过 file:///C:/JavaScript/Index.html 引用本地机器上的 .html 文件。

2 files(index.html and JS.js), both located locally on C:\\Javascript 2 个文件(index.html 和 JS.js),都位于本地 C:\\Javascript

HTML Code: HTML代码:

<!doctype html>
<head>
    <title>Title in browser tab</title>
</head>
<body>
"Text on the page"
<script src="C:\JavaScript\JS.js"></script>
</body>
</html>

-Based on what I read, if they are in the same folder I should be able to just reference <Script src="JS.js"> as there is no folder structure to look through, is that correct? -根据我读到的内容,如果它们在同一个文件夹中,我应该可以只引用<Script src="JS.js">因为没有要查看的文件夹结构,对吗?

-I've also tried to absolute path via <script src="file:///C:/JavaScript/JS.js"> and related versions of <Script src="C:\\Javascript\\JS.js"> that do not work either. - 我还尝试通过<script src="file:///C:/JavaScript/JS.js">和相关版本的<Script src="C:\\Javascript\\JS.js">获得绝对路径也不行。

In my JS.js file, I have nothing but alert();在我的 JS.js 文件中,我只有alert(); to test functionality, as my reasoning for incorrect sourcing.测试功能,作为我对错误采购的推理。 If I simply write <script>alert();</script> without referencing any outside source, the alert works as planned.如果我只编写<script>alert();</script>而不引用任何外部来源,警报会按计划工作。

Thank you in advance!先感谢您!

I'd recommend popping open Chrome's Developer Tools to see where the issue may lie (and, if you're new to development, these are tools that are built into Chrome that will make your life so much easier).我建议你突然睁Chrome的开发者工具,看看那里的问题可能在于(如果你是新来的发展,这些都是内置到Chrome浏览器的工具,这将使你的生活变得更轻松)。

Your assumption about not requiring a path should be correct: if you're referencing another file that lives in the same directory, omitting a full path will cause the browser to assume the path is relative (eg "right next to") to the current file:您关于不需要路径的假设应该是正确的:如果您正在引用位于同一目录中的另一个文件,则省略完整路径将导致浏览器假设该路径是相对于当前路径的(例如“紧邻”)文件:

<!doctype html>
<head>
    <title>Title in browser tab</title>
</head>
<body>
"Text on the page"
<script src="JS.js"></script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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