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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11521011/
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-08-29 01:42:46  来源:igfitidea点击:

Why base tag does not work for relative paths?

htmltagsbase

提问by ALH

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

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

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

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

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

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

But when I open jQuery from firebug it shows:

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

<!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?

我到处寻找答案,但似乎我的工作做得对!那么问题是什么?

回答by Rudi Visser

/assets/jquery-1.7.1.min.jsis not relative but absolute*, the /takes it to the root even with a basetag.

/assets/jquery-1.7.1.min.js不是相对的而是绝对的*,/即使带有base标签也将其带到根。

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

如果您删除它/,它应该使它相对于当前路径,当存在base标签时,它将是http://localhost/framework/.

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

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

Full working example:

完整的工作示例:

<!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.

*实际上取决于你问的是谁,它仍然是相对的,因为它是相对于当前域的。但我更喜欢将其称为绝对路径,因为它表示基于当前域的路径来自根。虽然,我想从技术上讲,这使它在事物的宏伟计划中是相对的,而仅在当前领域方面是绝对的。任何。

回答by techfoobar

Try having your base tag like:

尝试使用您的基本标签,例如:

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

and your script tag like:

和你的脚本标签,如:

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