Search code examples
javascripturlencodehtml-encodepercent-encoding

Converting directly from HTMLEncoding to URLEncoding


I'm currently transfering a set of ASP classic pages over into our new MVC based intranet. While I'm not making any sigificant functionality changes I'm making some UI improvements and some more best practices. This is however throwing up problems with special characters.

One section works by a drop down list of clinical tests which then GETS via the querystring on the name of the test. However ASP.net's HTMLEncoding is being overly aggressive (despite being UTF-8 it's encoding a wide range of characters). This results in the HTML encoded character being URL encoded and the whole thing throwing a "potentially dangerous request" error.

I could not HTMLEncode the test names, the people with access to change this is quite limited, however that not a path I want to start going down.

This leaves me with unencoding the string then reencoding it, but JavaScript doesn't have a native HTMLDecode function. Rather than getting a long list of characters and running a replace over the string I was wondering if there was an easy way to go from an HTMLencoded character straight to a percent encoded character, ie rather than decode and reencode something along the lines of &xxx; = %(xxx/2)


Solution

  • I managed to come up with a function that takes an HTMLencoded character (using charcodes rather than entity names) and creates a JavaScript character, from here going to URLencoded or doing something else with it is trivial

    result = subject.replace(/&#(\d*);/img, function(match, matchinggroup) {
        return String.fromCharCode(matchinggroup); 
    });