An encoding workaround

For the last couple days, I’ve been tweaking URL encoding with UTF-8 characters using JavaScript. I got an interesting situation where when the hash part of the URL (i.e. “mystate_information” in this URL http://myserver/myapp.html#mystate_information) works fine when it’s being updated by the JavaScript or the state manager. When loading the URL directory from bookmark or in the address bar, the state sometimes would get decoded before reach my state handling code instead passing in raw string. This means that the state sometimes is encoded and sometimes isn’t. This makes coding a little challenging.

Here is a workaround that I figured out. Since URL encoded text has “%” characters, I look for this character. If it exists, do the decoding. If not, skip decoding. Here is a simple sample code:

var input = mystate_information;
if(input.indexOf('%') != -1)
   input = aDecodeFunction(mystate_information)

Do you know of a corner case that this might not work? Or do you have a smarter idea?

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s