Url encoding in Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Url encoding in Javascript

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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:
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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
:)
Post Reply