html src 和 href 属性的相对路径或 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7503130/
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
Relative path or url for html src and href attributes
提问by steampowered
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?
使用框架可以很容易地为我的 html src 和 href 属性列出完整的 url,我觉得我通过列出完整的 url 而不是相对路径更彻底。但这会更快吗?我是否需要进行额外的 DNS 查找?当内容在同一台服务器上时,最佳做法是什么?
<img src='http://site.com/images/img1.png' />
<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:
Codeigniter 的图像助手 img()在用户指南中的工作方式如下:
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:
和 Codeigniter 的锚助手 anchor()在用户指南中的工作方式如下:
echo anchor('news/local/123','My News');
// gives <a href="http://example.com/index.php/news/local/123" >My News</a>
回答by PeeHaa
'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.
基本上规则是内部 URL 应该是相对的。
回答by uotonyh
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.
就 DNS 而言,您是否拥有相对或绝对 URL 真的无关紧要。无论如何,您的浏览器最终都会将服务器 URI 放在前面。此外,您的网络堆栈会第一次执行查找,并缓存 IP。除非出现问题,否则每页应该只查找一次。当然是 YMMV,但这应该是所有工作的方式。
回答by Mike Robinson
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
- 如果你想在本地开发网站
- 您更改/添加域(开发、登台等)
- 您改用 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.
最重要的是,使用您的代码的其他开发人员会试图杀死您。这对您的健康不利。
回答by mothmonsterman
Portability would be the issue for me. I would choose the second option based on that alone.
便携性对我来说是个问题。仅凭这一点,我会选择第二个选项。