-
-
Save gwobcke/2773779 to your computer and use it in GitHub Desktop.
| <% | |
| FUNCTION URLDecoder(str) | |
| '// This function: | |
| '// - decodes any utf-8 encoded characters into unicode characters eg. (%C3%A5 = å) | |
| '// - replaces any plus sign separators with a space character | |
| '// | |
| '// IMPORTANT: | |
| '// Your webpage must use the UTF-8 character set. Easiest method is to use this META tag: | |
| '// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
| '// | |
| Dim objScript | |
| Set objScript = Server.CreateObject("ScriptControl") | |
| objScript.Language = "JavaScript" | |
| URLDecoder = objScript.Eval("decodeURIComponent(""" & str & """.replace(/\+/g,"" ""))") | |
| Set objScript = NOTHING | |
| END FUNCTION | |
| %> |
To respond to your comments:
-
You don’t need to use eval - I just decided to go down that path because I feel it’s cleaner.
-
JavaScript vs JScript: While Microsoft’s underlying engine is indeed JScript, passing "JavaScript" to ScriptControl.Language is fully supported. Windows maps "JavaScript" to the exact same COM ProgID under the hood, so both names work interchangeably here without any issues.
-
Using .replace(/+/g," ") is chained inside the string evaluation, meaning the replacement of “+” actually does execute before decodeURIComponent runs and safely preserves any %2B
-
decodeURIComponent natively expects UTF-8 encoded URIs - however, note I commented practical advice for Classic ASP developers in older ASP environments like CodePage 1252. If you decode a UTF-8 string and output it to a page that isn't explicitly set to UTF-8, it will most likely render those decoded characters (like å) as garbled text (mojibake).
In conclusion there is no functional difference to your suggestion and my original GIST.
There are a few things I'd like to point out:
EvalJScript, notJavaScript+signs must be replaced beforedecodeURIComponent,%2Bis a valid URL encoded character.strargument was encoded.