About the author

Steven HarmanSteven Harman is a passionate developer who believes that writing great software isn't just a job, its a craft.

ASP.NET MVP

For recent posts and more about me, scroll to the bottom.

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

News

devLINK Technical Conference

Jobs

Badges

  • Subtext Project
  • Support Subtext
  • HiddenNetwork.com Banner

URL and HTML Encoding on the Client? JavaScript to the Rescue!

Phil recently wrote about some of The Most Useful .NET Utility Classes Developers Tend to Reinvent Rather Than Reuse - an article chock full of tasty tips, tricks, and reminders about [.net] framework features you forgot (or never knew) existed.

One of the utility classes Phil mentioned was the System.Web.HttpUtility class. Two of the super useful methods this class offers are UrlEncode and UrlDecode... used to uh, convert a string into a URL encoded string and decode a URL encoded string, respectively.

Do you encode?

All web developers, regardless of language/platform, should be intimately familiar with the basic encoding schemes used on most web pages - namely Html Encoding and URL Encoding.

Hopefully your language/framework of choice provides a nice utility class to help you do URL and HTML encoding/decoding on the server. But what if you need to do some URL or HTML encoding/decoding on the client side - that is, in the browser?

JavaScript to the rescue!

Lucky for you JavaScript has a couple of global methods that will handle the URL encoding and decoding for you.

First, take a look at the encodeURI and decodeURI methods. These methods do almost exactly what you'd expect. I say almost because the encodeURI method does break the Principle of Least Surprise because it does not encode the following characters: ":", "/", ";", and "?". However, as far as I can tell the decodeURI method will correctly decode those characters.

And if you need to URL encode those characters, what then? Fear not... there is also an encodeURIComponent an it's counterpart, decodeURIComponent. These methods will do URL encoding and decoding just as you'd expect:

document.write(encodeURIComponent("http://www.tempuri.org"))
document.write("<br />")
document.write(encodeURIComponent("http://www.tempuri.org/p 1/"))

will result in the following output:

http%3A%2F%2Fwww.tempuri.org
http%3A%2F%2Fwww.tempuri.org%2Fp%201%2F

What about HTML encoding and decoding?

Unfortunately, JavaScript lacks built-in methods to do HTML encoding and decoding, but there are several JavaScript libraries which provide such functionality. My favorite is the Prototype JavaScript framework and it's escapeHTML and unescapeHTML methods, part of Prototype's String class.

With those methods in hand, you can encode and decode URL and HTML right on the client - which can be very useful in what is becoming a very Web 2.0+ world. Good Luck, and I hope this helps!

What others are saying.

# URL and HTML Encoding on the Client? JavaScript to the Rescue!
Gravatar DotNetKicks.com
Jun 17, 2007
You've been kicked (a good thing) - Trackback from DotNetKicks.com
# re: URL and HTML Encoding on the Client? JavaScript to the Rescue!
Gravatar Golf Scrambles
Jun 17, 2007
Friggin awesome tip, never thought about pushing that via Javascript!
# how i can add encoding on the client in our HTML code
Gravatar akshay
Feb 29, 2008
Hi ,

Could you please help me on how i can add encoding on the client in our HTML code.

Thanks
Comments have been closed on this topic.