< script>中的./和/之间有什么区别吗? src属性? [英] Is there any difference between ./ and / in <script> src attribute?

查看:194
本文介绍了< script>中的./和/之间有什么区别吗? src属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下项目结构:

/root
  /static
    script.js
  page.html

这将"导入" script.js到HTML文件:

This will 'import' script.js, into the HTML file:

<html>
    <head>
        <script src="/static/script.js"></script>
    </head>
    <body>
        ...
    </body>
</html>

这也将:

<html>
    <head>
        <script src="./static/script.js"></script>
    </head>
    <body>
        ...
    </body>
</html>

我想知道:

  1. 一种方法优先于另一种方法吗?
  2. /./<script>属性中,/./的行为会有所不同吗?
  1. Is one way preferred over the other?
  2. Are there any cases, when / and ./, in she src attribute of <script> will behave differently?

推荐答案

现在,我对JavaScript的了解不是很高,但是我会让你知道我所知道的.

Now, I am not super experienced in JavaScript, but I'll let you know what I know.

[...]
<script src="./static/script.js"></script>
[...]
<!--This would reference files in the current folder (where the webpage itself is stored)-->

[...]
<script src="/static/script.js"></script>
[...]
<!--This would reference an absolute path within your webserver, and cannot change dynamically based on from where you load it.-->

通常来说,当您从当前文件夹(和/或服务器)中的文件加载./时,我会选择./,而/似乎是对我的外部引用,这也不是动态的.如果您碰巧移动了文件(如果文件与页面位于同一目录中),我认为JavaScript也会引用新文件,而不是抱怨旧文件.

Generally speaking, I'd go for ./ when you load it from a file in your current folder (and/or server), whilst doing / seems like an external reference to me, which is also not dynamic. If you happen to move the file (if it was in the same directory as your page), I think JavaScript would also reference the new file instead of complaining about the old one.

我不能保证上面的任何信息都是正确的,因为我不是一个非常好的JS-Developer,但这至少应该可以帮助您进一步了解语法.

I cannot guarantee that any of the info above is correct as I am not a really good JS-Developer, but at least this should help you figure out the syntax a little more.

这篇关于&lt; script&gt;中的./和/之间有什么区别吗? src属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆