Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

url - XHTML and & (Ampersand) encoding

My website is XHTML Transitional compliant except for one thing: the & (ampersand) in the URL are written as it is, instead of &

That is, all the urls in my pages are usually like this:

<a href="http://www.foo.com/page.aspx?x=1&y=2">Foo</a>

But XHTML validator generates this error:

cannot generate system identifier for general entity "y"

... and it wants the url to be written like this:

<a href="http://www.foo.com/page.aspx?x=1&amp;y=2">Foo</a>

The problem is that IE and Firefox don't handle the URL correctly and ignore the y parameter. How can I make this link work and validate correctly?

It seems to me that it is impossible to write XHTML pages if the browsers don't work with strict encoded XHTML URLs.

Do you want to see in action? See the difference between these two links (copy and paste them as they are):

http://stackoverflow.com/search?q=ff&sort=newest

and

http://stackoverflow.com/search?q=ff&amp;sort=newest
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have just tried this. What you attempted to do is correct. In HTML if you are writing a link the & characters should be encoded as &amp; You would only encode the & as %26 if you wanted a parameter value to contain an ampersand. I just wrote a simple HTML page that contained a link: <a href="Default2.aspx?param1=63&amp;param2=hel">Click me</a> and it worked fine: default2.aspx received the parameters intended and the source passed validation.

The encoding of & as &amp; is required in HTML, not in the link. When the browser sees the &amp; in the HTML source for a link it will interpret it as an ampersand and the link target will be as intended. If you paste a URL into your browser address bar it does not expect it to be HTML and does not try to interpret any HTML encoding that it may contain. This is why your example links that you suggest we should copy/paste into a browser don't work and why we wouldn't expect them to work.

If you post a bit more of your actual code we might be able to see what you have done wrong, but you appear to be heading the right direction by using &amp; in your anchor tags.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...