P.Brian.Mackey
P.Brian.Mackey

Reputation: 44285

URLEncode - getting it to work how I want

I want to URL encode email addresses. I input "[email protected]" on w3schools, but it did not encode "@" or ".". I am using encodeURI() and have the same results. What is going on?

At least, I assume it did not encode because I see in FireBug Net tab:

GET http://dev:8989/SJMUserManager/Service/Index/[email protected]

I expected to see

GET http://dev:8989/SJMUserManager/Service/Index/bmackey%40foo%2Ecom

Upvotes: 1

Views: 8143

Answers (3)

whoughton
whoughton

Reputation: 1385

@ is a reserved character, and therefore not encoded, you can see the reference at: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI

Upvotes: 0

Alnitak
Alnitak

Reputation: 339955

@ and . are not special characters in HTTP URIs.

The characters that need encoding include space (which becomes +), + itself, and %, which is used to hex-encode.

Other characters outside the normal ASCII range of 32 - 127 and various other characters within that range use that % hex encoding.

For correct handling, you should consider using encodeURIComponent() but only on the part of the URI that was user supplied. If you encode the entire URI that way you'll get an invalid URI.

Upvotes: 5

Christian Tellnes
Christian Tellnes

Reputation: 2080

You can try using encodeURIComponent instead.

encodeURIComponent('GET http://dev:8989/SJMUserManager/Service/Index/[email protected]')

Return:

"GET%20http%3A%2F%2Fdev%3A8989%2FSJMUserManager%2FService%2FIndex%2Fbmackey%40foo.com"

Upvotes: 3

Related Questions