Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

href syntax : is it okay to have space in file name

it has always been my practice that when ever i use images i name them like walls_ico , bu_hover

so when i give paths they go like

<img src="images/walls_ico.ico" /> <img src="buttons/bu_hover.png" /> 

UNTIL now when i am on a project where users upload files...

i was wondering is it okay to have spaces between file and folders name like

<img src="buttons/bu hover.png" /> 
like image 587
Moon Avatar asked Nov 13 '10 12:11

Moon


People also ask

Can href have spaces?

Sure, spaces in an ˂a href˃ or ˂img src˃ make it invalid — but “invalid” doesn't mean what you think it means. As I explored in my last Product Blogs post, as a technical marketer, knowing when you must not encode is just as important as knowing when you must .

How do you add a space in href?

HTML URI/URL Encoding URLs use the ASCII charset. URL encoding replaces space characters with "%20" (percent followed by the ASCII code for a blank space).


2 Answers

The src attribute should contain a valid URL. Since space characters are not allowed in URLs, you have to encode them.

You can write:

<img src="buttons/bu%20hover.png" /> 

But not:

<img src="buttons/bu+hover.png" /> 

Because, as DavidRR rightfully points out in his comment, encoding space characters as + is only valid in the query string portion of an URL, not in the path itself.

like image 186
Frédéric Hamidi Avatar answered Sep 18 '22 21:09

Frédéric Hamidi


Quoting HTML5 to back Frederic that spaces are not allowed:

http://www.w3.org/TR/html5/links.html#attr-hyperlink-href:

The href attribute on a and area elements must have a value that is a valid URL potentially surrounded by spaces.

The definition of "valid URL" points to: http://url.spec.whatwg.org which defines URL code points https://url.spec.whatwg.org/#url-code-points:

The URL code points are ASCII alphanumeric, "!", "$", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "=", "?", "@", "_", "~", and code points in the ranges U+00A0 to U+D7FF, U+E000 to U+FDCF, U+FDF0 to U+FFFD, U+10000 to U+1FFFD, U+20000 to U+2FFFD, U+30000 to U+3FFFD, U+40000 to U+4FFFD, U+50000 to U+5FFFD, U+60000 to U+6FFFD, U+70000 to U+7FFFD, U+80000 to U+8FFFD, U+90000 to U+9FFFD, U+A0000 to U+AFFFD, U+B0000 to U+BFFFD, U+C0000 to U+CFFFD, U+D0000 to U+DFFFD, U+E1000 to U+EFFFD, U+F0000 to U+FFFFD, U+100000 to U+10FFFD.

The spec then uses the term URL code points on various parts of the parsing algorithm as:

If c is not the EOF code point, not a URL code point, and not "%", parse error.

for the scheme, authority, relative path, query state and fragment states: so the entire URL.