Are spaces in "href" valid?
In (X)HTML5, the a
element’s href
attribute
[…] must have a value that is a valid URL potentially surrounded by spaces.
As the anchor suggests, it may contain leading and trailing spaces.
The linked section makes clear that these spaces will be stripped:
[…] the user agent must remove all space characters that are at the start or end of the string […]
If the actual URL starts/ends with (or contains) spaces, you have to percent-encode them with %20
.
These elements have different URL values:
<a href=" foo">…</a> <!-- the URL is <foo> -->
<a href="%20foo">…</a> <!-- the URL is < foo>, i.e., <%20foo> -->
These elements have the same URL value:
<a href="%20foo">…</a>
<a href="%20foo ">…</a>
<a href="%20foo ">…</a>
<a href=" %20foo">…</a>
<a href=" %20foo">…</a>
<a href=" %20foo ">…</a>
Related videos on Youtube
porton
I am a man with passion of IT and mathematics since childhood and an experienced programmer. My favorite programming languages are D and Ada, but I am quite well with Python, C#, PHP, Java, etc., too. I am a long time experienced Linux user.
Updated on September 18, 2022Comments
-
porton over 1 year
Is
<a href=" #id ">
(note spaces) correct (X)HTML?Does it mean the same as
<a href="#id">
?