AnApprentice AnApprentice - 5 months ago 16
Javascript Question

Is it possible to base 36 encode with JavaScript / jQuery?

I'm thinking about using the encode/decode technique here (Encoding to base 36/decoding from base 36 is simple in Ruby)

how to implement a short url like urls in twitter?

Idea being to track user referrals, invite URLs. I can use Rails to decode, but it there a way to encode with Javascript or jQuery?

Thanks

Answer

The toString method on Number has an optional argument of radix:

(128482).toString(36);
128482..toString(36);
128482 .toString(36);
var num = 128482; num.toString(36);

Note this doesn't work, because numbers expect decimal digits after a period, not letters:

128482.toString(36);  // Syntax error

Also, you can decode with JS as well:

parseInt("2r4y", 36);
Comments