Two of the most widely used JavaScript functions that are now deprecated are the escape() and unescape() functions. This means that in order to ensure that your code will continue to work in all future browsers, you should use one of the alternative functions as was indicated on MDN:

  • encodeURI()
  • decodeURI()
  • encodeURIComponent()
  • decodeURIComponent()

What To Do When They Are No More

Since these functions may not always exist, I decided to write the necessary JavaScript code to make sure they can continue to be used:

(function() {
var objGlobal = this;
if(!(objGlobal.escape && objGlobal.unescape)) {
  var escapeHash = {
    _ : function(input) {
      var ret = escapeHash[input];
      if(!ret) {
        if(input.length - 1) {
          ret = String.fromCharCode(input.substring(input.length - 3 ? 2 : 1));
        }
        else {
          var code = input.charCodeAt(0);
          ret = code < 256
            ? "%" + (0 + code.toString(16)).slice(-2).toUpperCase()
            : "%u" + ("000" + code.toString(16)).slice(-4).toUpperCase();
        }
        escapeHash[ret] = input;
        escapeHash[input] = ret;
      }
      return ret;
    }
  };
  objGlobal.escape = objGlobal.escape || function(str) {
    return str.replace(/[^\w @\*\-\+\.\/]/g, function(aChar) {
      return escapeHash._(aChar);
    });
  };
  objGlobal.unescape = objGlobal.unescape || function(str) {
    return str.replace(/%(u[\da-f]{4}|[\da-f]{2})/gi, function(seq) {
      return escapeHash._(seq);
    });
  };
}
})();

Reference

I created these functions based on information found at DevGuru.com.


10 Comments

onemen · October 7, 2011 at 6:45 PM

error in line
ret = String.fromCharCode(input.substring(input.length – 3 ? 2 : 1));

need to be
ret = String.fromCharCode(parseInt(input.substring(input.length – 3 ? 2 : 1),16));

Edgar Martinez · July 25, 2013 at 12:56 AM

Thank you for the post I was looking for what is the new standard approach to this.

Cave Man · August 11, 2013 at 8:55 AM

Who is the genius (idiot) who decided to deprecate such a useful function. There are some stupid people in high places 🙁 Uhhhh

    Luis Romero · September 2, 2013 at 9:27 AM

    the same people who deprecated tag :'(

Madhu Sudhan · February 17, 2014 at 6:57 AM

The escape and unescape functions are used to send and retrieve AJAX methods flexibly. The methods solve my problem with AJAX call from scripting. I am asking why we need to deprecated them?

    Chris West · February 17, 2014 at 10:31 PM

    I understand where you are coming. Besides using the function that I defined a while back, an even better solution would be to use something like encodeURIComponent.

Per Liedman · September 16, 2014 at 9:20 AM

Thanks for sharing this useful fallback! I need escape in a project where the server expects ISO-8859-1 encoded URIs, and I don’t have power to change this.

A tiny improvement: escape and unescape works even if the input is not a string (typically, passing a number), but your functions break since numbers don’t have the replace method.

Changing line 24 and 29 to:

return str.toString().replace […]

fixes this.

Gustav Persson · October 13, 2014 at 11:11 AM

Thanks! encodeURIComponent isn’t working in my case so this I just was I was looking for.

pizza steve · July 3, 2016 at 5:53 AM

This is not the exact ecma implementation. This is probably better, and yet, I’m dealing with some script that hashes god knows what and the result is not correct, had to write my own .

Nd · February 21, 2018 at 3:38 AM

base 64 utf-8 strings sent from php in XMLHttpRequest…

PHP: base64_encode(str);
JS:
function base64_decode(str)
{
return decodeURIComponent(escape(atob(str)));
}

Really it must be deprecated???

Leave a Reply

Your email address will not be published. Required fields are marked *