Page 1 of 1

Url encoding in Javascript

Posted: Fri Feb 17, 2006 9:30 am
by Skittlewidth
I've been getting into AJAX recently and I've stumbled upon a simple problem.

I need to be able to pass strings potentially containing

Code: Select all

* @ - _ + . /
via the query string, characters which escape() alone won't convert to the Hex equivalent.

Googling has so far come up with a number of sites that provide converters where you paste your string and marvel at their encoding magic, but none of them have made their code public. I have looked into the source of one page but I'm reluctant to copy the code cos its more than I need and I don't fully understand it anyway.

Can someone explain the very basics of converting just the above characters into url safe codes?

I know it will involve String.charCodeAt and String.fromCharCode but thats about as much as I understand.

Posted: Fri Feb 17, 2006 9:40 am
by Chris Corbyn
escape() :)

Code: Select all

var foo = escape('foo bar?xghigyt');

alert(foo);
EDIT | Scrap that, then slap me with a wet fish for not reading your post completely :oops:

Posted: Fri Feb 17, 2006 9:48 am
by Skittlewidth
*slap*:lol:

No worries, I found what I was looking for in the end.

The source of http://www.albionresearch.com/misc/urlencode.php gave permission to use their functions, and since I can't credit them on the thing I'm building, except in the source, I'll give them a plug here
:)