SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Location
    Tokyo
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using & instead of just &

    On page 89 of "Database Driven Web Site" the author writes:

    This time, our link passes two variables: firstname and lastname. The variables are separated in the query string by an ampersand (&, which must be written as & in HTML). You can pass even more variables by separating each name=value pair from the next with an ampersand.

    It works without using & as well by just directly using ampersands. I just tried it. In fact, for years now in scripting I have always written the ampersands directly when defining a URL string with multiple URL parameters.

    Does anybody think there is anything really wrong with doing that?

    It seems cumbersome to use an HTML entity each time you need to write an ampersand if that isn't necessary.

    Thanks,

    doug

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Using & works just fine, but your code won't pass validation.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Location
    Tokyo
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Using & works just fine, but your code won't pass validation.
    Interesting. I hadn't thought of that. Which validation will it fail - any and all?

    doug

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Don't know
    Maybe someone else?

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Location
    Tokyo
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Don't know
    Maybe someone else?
    Sure enough you're right! I just tried it with the w3.org validator and got this error message:

    Entity references start with an ampersand (&) and end with a semicolon. If you want to use a literal ampersand in your document you must encode it as "&" (even inside URLs!).

    I can see why that is reasonable - because the & marks the beginning of an HTML entity. So it is more consistent (even though more cumbersome) to encode the & marks in URLs that way.

    doug@learning something new every day

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,804
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If the value following an & evaluates as an entity code then it will get converted into the code and you will not have your apersand there any more. So it isn't just that it fails validation, it may also end up converting to something completely different than you wanted to have there. For example if the next field is called "copy" and you place &copy in your URL then you end up with a copyright symbol in place of the text that you wanted.
    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="^$">

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just because you've gotten away with it, doesn't mean it's right. As mentioned use &amp; in the source


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
  •