Should query string parameter values in a URI have single quotes around them?

10,125

<a href="link.php?a=first&b=second">Click here!</a>

The only thing that might get flagged in that anchor is the unencoded &. The & should be HTML encoded. ie. &amp; (in order to avoid potential conflict). URL parameters certainly should not be quoted as a general rule.

The URL, by its very nature, is all a "string", so quoting URL parameter values for the sake of it does not make sense, unless you explicitly needed this for your script (although this should probably be avoided, as it just adds clutter). If you quote a URL parameter value then the quotes themselves become part of the value, they do not delimit the value itself.

Share:
10,125

Related videos on Youtube

user58934
Author by

user58934

Updated on September 18, 2022

Comments

  • user58934
    user58934 over 1 year

    I'm passing variables through the URI string, like this:

    example.com?a=first&b=second
    

    Everything works as intended, but when I pass the source code through the W3 validator, it's flagging them as errors. When I quote the variables, like this:

    example.com?a='first'&b='second'
    

    they do not get flagged. I've never seen a website do this, but is that the proper way to pass these variables? I could not find any source that recommends quoting them in the URI.

    • Stephen Ostermiller
      Stephen Ostermiller over 8 years
      What are you validating? The URL with the parameters or a page that links to it?
    • user58934
      user58934 over 8 years
      I'm validating the page that contains the links. So, the code that's getting flagged would be something like: <a href="link.php?a=first&b=second">Click here!</a> But it doesn't get flagged when written like this: <a href="link.php?a='first'&b='second'">Click here!</a>
    • MrWhite
      MrWhite over 8 years
      That doesn't make much sense, can you post the exact code and error message that the W3C Validator is reporting? Also (if it matters) what DOCTYPE are you validating to?
    • user58934
      user58934 over 8 years
      Here's the actual code: <a href="../show_initiative.php?id=19&a=yes"> The validator throws four errors: "No space between attributes," "Saw " when expecting an attribute name. Probable cause: = missing immediately before.", "Attribute " is not serializable as XML 1.0.", and "Attribute " not allowed on element a at this point." The page is HTML5. The validator reports: "The Content-Type was text/html. Using the HTML parser." and "Using the schema for HTML with SVG 1.1, MathML 3.0, RDFa 1.1, and ITS 2.0 support."
    • user58934
      user58934 over 8 years
      Thanks, all! A follow up, for those who are interested. This line gets flagged: <a href="../show_initiative.php?id=19&a=yes">, but these (yes, both of them) do not: <a href="../show_initiative.php?id=19&amp;a=yes"> and <a href="../show_initiative.php?id='19'&a='yes'">
  • user58934
    user58934 over 8 years
    Thanks for the tip--you've answered my question. I like to keep things simple, and in this case, it seems that I can.