Html, Url & Xml encoding page and script

by Jason Haley 30. September 2006 16:02

Lately I've been reading David Flanagan's JavaScript: The Definitive Guide on my commute.  It is a must have if you are a web developer!

A couple of weeks ago I started some encoding scripts about the same time I was reading the chapter on how to create namespaces is JavaScript, but didn't have the time to finish the work until today.

I have updated my Encoding Page to now have decoding and to use a new JH.Utilities javascript class (download here).

The functions in this class are:

JH.Utilities.HtmlEncode(textToEncode) - replaces the common English entities with their html version (ie. & = &)

JH.Utilities.HtmlDecode(textToDecode) - replaces the common html entities with their English version (ie. & = &)

JH.Utilities.HtmlEncode2(textToEncode) - replaces the characters with their html escaped version (ie. & = &)

JH.Utilities.HtmlDecode2(textToDecode) - replaces the escaped characters with their English characters (ie. & = &)

JH.Utilities.UrlEncode(textToEncode) - replaces the characters with their escaped Url version (ie. a space = %20)

JH.Utilities.UrlDecode(textToDecode) - replaces the escaped Url characters with their English version (ie. %20 = a space)

JH.Utilities.XmlEncode(textToEncode) - replaces the common English entities with their Xml version (ie. & = &)

JH.Utilities.XmlDecode(textToDecode) - replaces the escaped characters with their common English version (ie. & = &)

NOTE: I have only briefly tested these scripts, so if you are going to take them and use them - beware there are probably bugs in them.

If you just need to Url encode a string or decode an Url, you should use javascript's functions such as encodeURI/decodeURI, encodeURIComponent/decodeURIComponent or the older escape/unescape.  The only reason I implemented my own was for the sure joy of it :)

Comments (4) | Post RSSRSS comment feed |

Categories:
Tags:

Comments

Comments are closed