Html 我可以在有空格的 URL 中用 & 替换 % 20 吗?

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

Can I replace % 20 with & nbsp in URLs that have spaces?

htmlurlencodingcharacter-encoding

提问by Pup

Within my HTML, can I use the character entity reference " " in place of "%20" in Web URLs?

在我的 HTML 中,我可以在 Web URL 中使用字符实体引用“  ”代替“ %20”吗?

They're both spaces, right?

他们都是空间,对吧?

回答by Laurence Gonsalves

The short answer is, they are both used to represent "spaces", but they represent differentspaces.

简短的回答是,它们都用于表示“空间”,但它们代表不同的空间。

%20is the URL escaping for byte 32, which corresponds to plain old space in pretty much any encoding you're likely to use in a URL.

%20是字节 32 的 URL 转义,它对应于您可能在 URL 中使用的几乎任何编码中的普通旧空间。

 is an HTML character reference which actually refers to character 160 of Unicode (and also ISO-8859-1 aka Latin-1). It's a different space character entirely -- the "non-breaking space". Even though they look pretty much the same, they're different characters and it's unlikely that your server will treat them the same way.

 是一个 HTML 字符参考,它实际上是指 Unicode 的字符 160(还有 ISO-8859-1 aka Latin-1)。这是一个完全不同的空间字符——“不间断空间”。尽管它们看起来几乎相同,但它们是不同的角色,您的服务器不太可能以相同的方式对待它们。

回答by Sampson

No. Neither are spaces (technically). Both representspaces in different ways though. Make every effort to NOT have spaces, or representatives of spaces, in your URLs. Many find it more elegant (me included) to replace spaces with _ or -

不。空格也不是(技术上)。两者都以不同的方式表示空间。尽一切努力在您的 URL 中没有空格或空格的代表。许多人发现用 _ 或 - 替换空格更优雅(包括我在内)

回答by Aric TenEyck

No.   is an HTML non-breaking-space entity; this entity has no meaning when used in a filesystem or wherever else that a URL might point. URLs are not encoded in HTML.

号   是一个 HTML 不间断空格实体;当在文件系统或 URL 可能指向的任何其他地方使用时,该实体没有任何意义。URL 不是在 HTML 中编码的。

回答by Tomalak

No, not in the URLs. What you cando is replace spaces in the textual representation of the URL.

不,不在 URL 中。您可以做的是替换 URL 文本表示中的空格。

So instead of:

所以而不是:

<a href="http://some.site/doc%20with%20spaces">http://some.site/doc%20with%20spaces</a>

you can have:

你可以有:

<a href="http://some.site/doc%20with%20spaces">http://some.site/doc&nbsp;with&nbsp;spaces</a>

回答by ylebre

%20 is what you get with URL encoding, so this is what you should use if you are going to use it in a URL.

%20 是您通过 URL 编码得到的,因此如果您要在 URL 中使用它,这就是您应该使用的。

&nbsp; is a HTML entity, which is what should be used for 'non breaking space' in an HTML document.

  是一个 HTML 实体,它应该用于 HTML 文档中的“不间断空格”。

回答by Janusz

Most persons try to absolutely avoid spaces in their filenames in URLs. They will give you a serious headache every time so try to do so.

大多数人都尽量避免在 URL 的文件名中使用空格。他们每次都会让你头疼,所以尽量这样做。

If you want to have spaces in an URL you have to encode them with %20.

如果你想在 URL 中有空格,你必须用 %20 对它们进行编码。

&nbsp is used by the browser to know how to display the page. This information is only used for displaying. The %20 will be sent to the server that manages all the stuff needed to transfer the webpage to your visitors. The server doesn't speak html so the server would interpret &nbsp as a normal part of the filenname and search for a file called in the way foo bar. This file will not be found. Much worse the web server will think that the & begins the variable part of the url and only search for the page foo and then try to generate a variable nbsp and a variable bar but he want see any values for them. All in all the web server can't handle a URL with an   in it.

  被浏览器用来知道如何显示页面。此信息仅用于显示。%20 将发送到服务器,该服务器管理将网页传输给访问者所需的所有内容。服务器不会说 html,因此服务器会将   解释为文件名的正常部分,并搜索以 foo bar 方式调用的文件。将找不到此文件。更糟糕的是,Web 服务器会认为 & 开始 url 的变量部分,只搜索页面 foo,然后尝试生成变量 nbsp 和变量 bar,但他想查看它们的任何值。总而言之,Web 服务器无法处理带有 in 的 URL。

回答by Janusz

Neither are spaces. You shouldnt be using spaces but if for what ever reason you can't avoid it you should just be able to do...

空格也不是。您不应该使用空格,但是如果出于任何原因您无法避免它,您应该能够做到......

<a href="Web Page.aspx">Hey there</a>

...clicking on which will automatically navigate the user to

...单击将自动将用户导航到

WebSite/Web%20Page.aspx