Sample code for 30+ languages & platforms
JavaScript

URL Encoding and Decoding

See more Encryption Examples

Demonstrates URL encoding and decoding.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

// To URL encoding a string:
var s = "Why a > b?";

var sb = new CkStringBuilder();
success = sb.Append(s);

// URL encode the string.
sb.Encode("url","utf-8");

// Show the URL encoded string:
var sEncoded = sb.GetAsString();
console.log(sEncoded);

// The result is:  Why%20a%20%3E%20b%3F

// If you prefer "+" instead of "%20" for SPACE chars:
var numReplaced = sb.Replace("%20","+");
console.log(sb.GetAsString());

// Output is:   Why+a+%3E+b%3F

// To decode:
sb.Decode("url","utf-8");
console.log(sb.GetAsString());

// Result is: Why a > b?