Does not seem to work on all browsers
Everyone advises that ampersands in URLs should be encoded, like so:
This seems to work fine in IE6.0 and Opera, but NN7 and Firefox don't encode the & (it does not make a difference whether location.href is in single quotes or double quotes) Instead, the browser address line shows:
On the other hand, this works fine in all browsers:
Major websites (ie., view the page source on Google) still use the latter method.. even though it is not technically HTML 4.01 valid!
I think there is a suggestion that server-side processes should also be prepaed to accept a colon as well as an ampersand. Not sure how many have implemented this.
I think what you want is %26, which is the URL-encoded hexadecimal escaped value for the ampersand; not &, which is the HTML named, character entity.
Parenthetically, it's easy to get URL-encoded values by just typing something like the following in your browser's location window:
I read that %26 should only be used when you want to use an ampersand in the URL but not as a key delimiter in a query string.
For example, I noticed that when I search in Google for "encoding & ampersand", then the resulting URL in the browser address bar is something like:
Ony the non-delimiting ampersand (and semi-colon) has been encoded. The page source for that Google search still shows that delimiting ampersands are NOT encoded.
There is confusion here.
Thanks guys. I was given confused by this previous thread [webmasterworld.com] which did discuss ampersands in location.href, but that was for xhtml.