Use encodeURIComponent when you want to encode the value of a URL parameter.

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).
[ Mozilla encodeURIComponent - Mozilla decodeURIComponent ]

Code
var uri = '?x=test';
var encoded = encodeURIComponent(uri);
> %3Fx%3Dtest

var decode = decodeURIComponent(encoded);
> ?x=test