views:

1111

answers:

7

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

They're both spaces, right?

+5  A: 

No. Neither are spaces (technically). Both represent spaces 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 -

Jonathan Sampson
Depending on your point of view, that's not quite correct. `%20` represents a space, but ` ` represents a *non-breaking* space, technically a separate character. So even if it were a good idea to use HTML escaping in URLs, this wouldn't work because it would replace one character with another, changing the meaning of the URL.
Ben Blank
How does that differ from my answer?
Jonathan Sampson
The don't just represent spaces in different ways, the represent different spaces.
Pete Kirkham
Yes. I'm aware of that. But they still represent spaces - which is what I said.
Jonathan Sampson
+4  A: 

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.

Aric TenEyck
You can have non-breaking spaces in filenames, at least in some filesystems. It's probably not a good idea, but saying "this entity has no meaning when used in a filesystem" is going a bit too far. It's just another character. (And if you're referring to the entity reference,  , he was talking about using this in HTML, so the browser would take care of converting the entity reference to the corresponding character.)
Laurence Gonsalves
+2  A: 

%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.

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

ylebre
+2  A: 

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

So instead of:

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

you can have:

<a href="http://some.site/doc%20with%20spaces"&gt;http://some.site/doc&amp;nbsp;with&amp;nbsp;spaces&lt;/a&gt;
Tomalak
Does anyone know why the ` ` that I have in the lower sample does not show up? It used to, I'm sure...
Tomalak
Because it was turned into a space! Use   for it to show up properly.
Nick Lewis
Snap! You really think that's the cause? ;-) Seriously - I thought that a "code" section was to display it's contents *verbatim*. And I thought it did that until recently. Now if I write ` `, I fear that this will show up at some point...
Tomalak
A: 

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
Chalkey
A: 

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.

If you want to have spaces in an URL you have to encode them with %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.

Janusz
The server doesn't speak HTML, but he was talking abut using this in his HTML. The browser *does* interpret character references in attributes, including URLs, and so the   will turn into a non-breaking space. The server will never see the ", for that matter).
Laurence Gonsalves
Ok so the server will see a normal white space. That is also a bad thing :)
Janusz
No, it'll see a non-breaking-space character. It's a different character from normal space.
Laurence Gonsalves
+2  A: 

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

%20 is 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.

&nbsp; 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.

Laurence Gonsalves