【问题标题】:HTML base tag equivalent for absolute path与绝对路径等效的 HTML 基本标记
【发布时间】:2018-08-08 08:05:31
【问题描述】:

想象一个位于http://example.com/myWebsite/ 的网站。

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

我知道可以为带有base 标签的相对链接执行此操作,但我希望保留相对链接的默认行为。

我可以在 head 中指定一些有用的东西吗?

【问题讨论】:

  • 我不确定您是否理解绝对 URL 是什么。绝对 URL 与任何东西都不相关。您不能使像 https://stackoverflow.comhttps://www.google.com 这样的 URL 与您自己的域相关。如果您希望保留相对 URL 的默认行为,请不要使用基本元素 - 它不是必需的。
  • @BoltClock 我希望使用/myPath/foo.ext 请求的任何资源都被理解为http://example.com/myWebsite/myPath/foo.ext
  • 目前我需要在每个绝对路径之前注入一个 baseUrl 变量 (== "example.com/myWebsite"),但是没有选项在某处指定一次感觉不对

标签: html path


【解决方案1】:
<!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>

使用这个

【讨论】:

  • 这将破坏相对链接行为。如果我在http://example.com/myWebsite/directory/index.html 上,任何像./image.png 这样的链接都会转换为http://example.com/myWebsite/image.png 而不是http://example.com/myWebsite/directory/image.png
猜你喜欢
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2021-08-15
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多