Today is:
    | Home | Web Technology | Language | Articles | | About-Us | Contact-Us |

JS Tutorial

    JS Articles

  • FAQ
    

JavaScript Frequently Ask Question


How to encode and decode a URL string in JavaScript?

by Puthyrak Kang
June 28, 2008
In some cases, you want to pass a query string as a part of URL. You have to ensure the query string does not contain any character that is reserved by the URL syntax. This can be done by using a JavaScript's built-in function escape to encode the query string.

Example:

function urlEncode(msg){
   return escape(msg);
}

Display

Incompatible string: "$5 + $5 = $10;"

To decode the encoded URL string back to regular string, you can use unescape function.

Example:

function urlDecode(msg){
   return unescape(msg);
}

Display

Encode URL string: "%245%20+%20%245%20%3D%20%2410%3B"


    References

    (1) Aland Shalloway & James R. Trott, Design Patterns Explained, Second Edition.

    (2) Allen Holub, Holub on Patterns, Learning Design Patterns by Looking at Code

    (3) Eric Evans, Domain-Driven Design, Tackling complexity in the heart of software.

    Advertisement

puthik.com ©2008