SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow & or & for URLs in xhtml anchor?

    When writing XHTML, specifically creating anchor tags for links, should you use html entities for characters in URL's like &?

    For instance... if I'm writing XHTML, which is correct?

    HTML Code:
    <a href="http://www.example.com/index.php?a=2&z=9">Link</a>
    or

    HTML Code:
    <a href="http://www.example.com/index.php?a=2&amp;z=9">Link</a>
    I never could quite figure this out. Thanks!

  2. #2
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    The second. An stand-alone ampersand is never allowed in HTML or XHTML, regardless of the location - it must always be the beginning of a character entity.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by C. Ankerstjerne View Post
    An stand-alone ampersand is never allowed in HTML or XHTML, regardless of the location
    That is not entirely true, Christian. In HTML (including pretend-XHTML) the style and script element types have a content model of CDATA. That means, among other things, that entity references are not parsed, so a literal ampersand must be 'stand-alone'.

    In (real) XHTML those elements have a content model of (#PCDATA), so entity references are parsed and literal ampersands must be escaped.
    Birnam wood is come to Dunsinane

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    So it is &amp; in PCDATA and & in CDATA regardless of whether you are using HTML or XHTML. With XHTML you can of course use CDATA tags to specify that scripts and styles are to be CDATA instead of PCDATA so as to be able to specify & instead of &amp;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    That is not entirely true, Christian. In HTML (including pretend-XHTML) the style and script element types have a content model of CDATA. That means, among other things, that entity references are not parsed, so a literal ampersand must be 'stand-alone'.

    In (real) XHTML those elements have a content model of (#PCDATA), so entity references are parsed and literal ampersands must be escaped.
    Fair enough - though I would argue that the contents inside the script and style elements is not HTML as such, even if it's inside an HTML document.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  6. #6
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,888
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by TantekHack View Post
    When writing XHTML, specifically creating anchor tags for links, should you use html entities for characters in URL's like &?

    For instance... if I'm writing XHTML, which is correct?

    HTML Code:
    <a href="http://www.example.com/index.php?a=2&z=9">Link</a>
    or

    HTML Code:
    <a href="http://www.example.com/index.php?a=2&amp;z=9">Link</a>
    The second one. If you run your website through the W3 validator, it will give you an error for the first one as you have not encoded the ampersand.

    Although it might sound picky, if you don't do that, you might find that your URL gets somewhat mangled if you haven't chosen your parameters with care...

    For example, let's say you run website.com, and people can search for products (with the prod argument), and can specify the section of your site it is in (with the sect argument).

    The URL should be, eg, http://website.com/search?maxprice=500&amp;sect=printer&amp;prod=HP
    but if you don't encode those ampersands properly, so browsers will parse it as
    http://website.com/search?maxprice=500=printer∏=HP
    because they think your URL includes the &sect; and &prod; entities.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •