-1

I'm trying to include a link in my page that will link directly to google search results (as opposed to linking to a predetermined search, as in this question).

I tried this:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
</head>

<body>
    <a href="http://www.google.com&amp;q=sports">click me</a>
</body>

</html>

The ampersand gets mangled, the url comes out as http://www.google.com%26q%3Dsports instead of http://www.google.com&q=sports.

What's the right way to do this?

Labrador
  • 623
  • 7
  • 13

1 Answers1

1

I think this is format if you want to return results in the correct manner:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
    <a href="http://www.google.com/search?q=sports">click me</a>
</body>

</html>

But here is the good resource and a duplicate to this question if that's the case - Do I encode ampersands in <a href...>?

  • Yes I confused '?' with '&'. Don't know how I did that. OTOH still not clear why the url I linked didn't show up as `http://www.google.com&q=sports` in the browser bar, but showed up as `http://www.google.com%26q%3Dsports` instead. Didn't I encode it right? – Labrador Jun 08 '20 at 20:02
  • Yes, I believe you did. Check the link that I've posted for more info – HoratioNullbuilt Jun 08 '20 at 20:22
  • Thanks, I had already included the link you posted in my own post. (I guess you hadn't noticed.) But I don't see how it explains why `http://www.google.com&q=sports` is not a proper `href` value, or at least why Chrome changes that url to `http://www.google.com%26q%3Dsports`. – Labrador Jun 08 '20 at 21:29
  • Well, if you change = to &equals; then you should see the right or should I say wrong results - a page that can't be retrieved by search results :) You can try this tool (https://validator.w3.org/) which clearly shows that in your particular example, = was escaped as well and it falls into the category of reserved chars withing a query component which have special meaning in a URL (https://tools.ietf.org/html/rfc3986#section-2.2) – HoratioNullbuilt Jun 08 '20 at 22:32
  • Thanks but changing `=` to `&equals;` led to the same mangled URL. I'm giving up, anyway, the other URL gives me the search I wanted... – Labrador Jun 09 '20 at 16:56