html src 和 href 属性的相对路径或 url

发布于 2024-12-05 15:35:26 字数 811 浏览 1 评论 0原文

使用框架可以轻松列出 html src 和 href 属性的完整 url,并且我觉得通过列出完整 url 而不是相对路径更加彻底。但这更快吗?我是否需要进行额外的 DNS 查找?当内容位于同一服务器上时,最佳实践是什么?

<img src='/images/img1.png' />

Codeigniter 的图像助手 img() 的工作方式类似 这来自用户指南

echo img('images/picture.jpg');
// gives <img src="http://site.com/images/picture.jpg" />

Codeigniter 的锚点帮助器anchor() 的工作方式类似于 来自用户指南

echo anchor('news/local/123','My News');
// gives <a href="http://example.com/index.php/news/local/123" >My News</a>

Using a framework makes it easy to list full url's for my html src and href attributes, and I feel I'm being more thorough by listing a full url instead of a relative path. But is this faster? Am I incurring an extra DNS lookup? What is the best practice when the content is on the same server?

<img src='http://site.com/images/img1.png' />

vs

<img src='/images/img1.png' />

Codeigniter's image helper img() works like this from the users' guide:

echo img('images/picture.jpg');
// gives <img src="http://site.com/images/picture.jpg" />

and Codeigniter's anchor helper anchor() works like this from the users guide:

echo anchor('news/local/123','My News');
// gives <a href="http://example.com/index.php/news/local/123" >My News</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橙幽之幻 2024-12-12 15:35:26

就 DNS 而言,您拥有相对 URL 还是绝对 URL 并不重要。无论如何,您的浏览器最终都会将服务器 URI 预先添加到前面。此外,您的网络堆栈首次进行查找并缓存 IP。除非出现问题,否则每页应该只有一次查找。当然是 YMMV,但这应该就是这一切的运作方式。

As far as DNS goes, it really doesn't matter if you have relative or absolute URL. Your browser ends up pre-pending the server URI onto the front anyway. Also, your network stack does the lookup for the first time, and caches the IP. Unless something goes wrong, there should only be the one lookup per page. YMMV of course, but that should be how this all works.

梦萦几度 2024-12-12 15:35:26

“从不”(几乎从不)使用绝对路径。

稍后它会咬你的屁股。

例如,当您切换/添加另一个域时。

从测试服务器转到生产服务器。

基本上规则是内部 URL 应该是相对的。

'Never' (alsmost never) use absolute paths.

It will bite you in the ass later.

For example when you switch / add another domain.

Go from your test to production server.

Basically the rule is internal URL's should be relative.

云朵有点甜 2024-12-12 15:35:26

哦,你真的不想使用完整路径。您将面临很多工作:

  • 如果您想在本地开发站点
  • 您更改/添加域(开发、登台等)
  • 您切换到使用 CDN

您还将破坏您的开发环境,因为大多数现代环境将执行本地目录查找。不能用域来做到这一点。

此外,在开发环境中,您将从生产站点拉取,这将使修改和添加图像变得极其棘手。

最重要的是,其他使用你的代码的开发人员会试图杀死你。这对你的健康有害。

Oh you really don't want to use a full path. You'll have a lot of work ahead of you:

  • If you want to develop the site locally
  • You change / add domains (development, staging, etc)
  • You switch to using a CDN

You also will break your dev environment, since most modern ones will perform local directory lookups. Can't do that with a domain.

Also, in a dev environment you will be pulling from the production site, which will make modifying and adding images extremely tricky.

Most importantly, other developers working with your code will try to kill you. And that's bad for your health.

零度℉ 2024-12-12 15:35:26

可移植性对我来说是个问题。仅凭这一点我就会选择第二个选项。

Portability would be the issue for me. I would choose the second option based on that alone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文