为什么基本标签不适用于相对路径?

2022-01-18 00:00:00 tags html base

我在页面的 <head> 部分有一个 <base> 标记,如下所示:

I have a <base> tag as below in <head> section of the page:

<base href="http://localhost/framework">

下面是一个相对的脚本(当然在<base>之后):

And a script as below which is relative (of course after <base>):

<script src="/assets/jquery-1.7.1.min.js">

但是当我从 firebug 打开 jQuery 时,它显示:

But when I open jQuery from firebug it shows:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body> 
Blah Blah Blah....

当我使用下面的链接时没关系:

When I use the below link it's OK though:

<script src="http://localhost/framework/assets/jquery-1.7.1.min.js">  

我到处寻找答案,但看来我的工作做得对!那么问题出在哪里?

I looked for an answer everywhere, but it seems I'm doing my job right! So what is the problem?

推荐答案

/assets/jquery-1.7.1.min.js 不是相对的而是绝对的*,/ 即使使用 base 标记也将其带到根目录.

/assets/jquery-1.7.1.min.js is not relative but absolute*, the / takes it to the root even with a base tag.

如果您删除该 /,它应该使其相对于当前路径,当 base 标记存在时,该路径将是 http://本地主机/框架/.

If you remove that /, it should make it relative off the current path, which, when a base tag is present would be http://localhost/framework/.

注意:您还需要在 href 的末尾添加一个尾随 /,以表明它是一个文件夹.

Note: You will also need to add a trailing / to the end of the href, to indicate that it's a folder.

完整的工作示例:

<!doctype html>
<html>
<head>
<base href="/test/" />
<script src="assets/test.js"></script>
<body>
hi
</body>
</html>

* 实际上取决于你问的是谁,它仍然是相对的,因为它与当前域无关.但我更喜欢称它为绝对,因为它表示路径来自根,基于当前域.虽然,我想从技术上讲,这使得它在宏伟的计划中是相对的,而仅就当前领域而言是绝对的.随便.

* Actually depending on who you ask, it's still relative since it is relative off the current domain. But I prefer to call this absolute since it's signifying the path is from the root, based on the current domain. Although, I guess technically that makes it relative in the grand scheme of things, and absolute only in terms of the current domain. Whatever.

相关文章