javascript 为本地开发环境编写脚本 'src' url 的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16677095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:35:54  来源:igfitidea点击:

What is the right way to write my script 'src' url for a local development environment?

javascriptlocalhostsrc

提问by Spilot

I'm working on a local environment and I'm not sure if I've written my src URl correctly because my functions aren't working. The bold script tag has the src in question.

我正在本地环境中工作,但我不确定我是否正确编写了 src URl,因为我的功能无法正常工作。粗体脚本标签有问题的 src。

<!DOCTYPE html>
<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script src="http://www.parsecdn.com/js/parse-1.2.2.min.js"></script>

    **<script src="/Users/myUserName/Desktop/myPage.js"></script>**

    </head>
    <body>
        <div id="mainDiv">
            <p><a href="#">anchor</a></p>
        </div>
    </body>
</html>

回答by ARC

Write the src tag for calling the js file as

编写调用js文件的src标签为

<script type='text/javascript' src='../Users/myUserName/Desktop/myPage.js'></script>

This should work.

这应该有效。

回答by dm03514

I believe the browser is looking for those assets FROM the root of the webserver. This is difficult because it is easy to start developing on your machine WITHOUT actually using a webserver ( just by loading local files through your browser)

我相信浏览器正在从网络服务器的根目录中寻找这些资产。这很困难,因为很容易在您的机器上开始开发,而无需实际使用网络服务器(只需通过浏览器加载本地文件)

You could start by packaging your html and css/js together?

您可以先将 html 和 css/js 打包在一起吗?

a directory structure something like:

目录结构类似于:

-yourapp
  - index.html
  - assets
    - css
    - js
      - myPage.js

Then your script tag (from index.html) could look like

然后你的脚本标签(来自 index.html)可能看起来像

<script src="assets/js/myPage.js"></script>

<script src="assets/js/myPage.js"></script>

An added benifit of packaging your html and assets in one directory is that you can copy the directory and give it to someone else or put it on another machine and it will work great.

将您的 html 和资产打包在一个目录中的另一个好处是,您可以复制该目录并将其提供给其他人或将其放在另一台机器上,它会很好地工作。

回答by insertwittyhandle

This is an old post but...

这是一个旧帖子,但...

You can reference the working directory (the folder the .html file is located in) with ./, and the directory above that with ../

您可以使用 引用工作目录(.html 文件所在的文件夹)./,以及使用其上方的目录../

Example directory structure:

示例目录结构:

/html/public/
- index.html
- script2.js
- js/
   - script.js

To load script.js from inside index.html:

从 index.html 中加载 script.js:

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

This goes to the current working directory (location of index.html) and then to the js folder, and then finds the script.

这将转到当前工作目录(index.html 的位置),然后转到 js 文件夹,然后找到脚本。

You could also specify ../to go one directory above the working directory, to load things from there. But that is unusual.

您还可以指定../转到工作目录上方的一个目录,从那里加载内容。但这是不寻常的。