绝对路径的HTML基础标签等效实现方式是什么?

5

假设有一个网站位于http://example.com/myWebsite/

我希望我的绝对路径指向myWebsite目录。

我知道可以使用base标记来处理相对链接,但我希望保留相对链接的默认行为。

在头部是否有什么有用的东西可以指定?


我不确定你是否理解绝对URL的含义。绝对URL与任何东西都无关。你不能将像https://stackoverflow.comhttps://www.google.com这样的URL相对于你自己的域名。如果你希望保持相对URL的默认行为,只需不使用基本元素 - 它并非必需。 - BoltClock
4
@BoltClock,我希望任何使用“/myPath/foo.ext”请求的资源均被视为“http://example.com/myWebsite/myPath/foo.ext”。 - Apolo
1
目前我需要一个baseUrl变量(==“http://example.com/myWebsite”)在每个绝对路径之前注入,但是没有指定它的选项感觉不对。 - Apolo
1个回答

2
<!DOCTYPE html>
<html>
<head>
<base href="http://example.com/myWebsite/" target="_blank">
</head>
<body>

<p><img src="stickman.gif" width="24" height="39" alt="Stickman"> - 
Notice that we have only specified a relative address for the image. 
Since we have specified a base URL in the head section, the browser will 
look for the image at "http://example.com/myWebsite/stickman.gif".
</p>

</body>
</html>

使用此


1
这将会破坏相对链接的行为。如果我在 http://example.com/myWebsite/directory/index.html 页面上,任何类似 ./image.png 的链接都会被翻译成 http://example.com/myWebsite/image.png 而不是 http://example.com/myWebsite/directory/image.png - Apolo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接