0

I am trying to decode a string not URL. i tried escape function which is working fine. but Escape function is deprecated now

var a = "mysap'fff'"; //it could be any string

var usingEscape = escape(a);
alert(usingEscape);

Result is : mysap%27fff%27 which is fine

var usingEncodeUri = encodeURI(a);
alert(usingEncodeUri);

Result is : mysap'fff

var usingEncodingUriComponemt = encodeURIComponent(a);
alert(usingEncodingUriComponemt);

Result is : mysap'fff

Now i need result which is given by escape function. Any alternate for the same

thanks

Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
Hitu Bansal
  • 2,917
  • 10
  • 52
  • 87
  • http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery try this – Viswanath Polaki May 20 '15 at 04:52
  • escape still works for me – Ritesh Karwa May 20 '15 at 04:53
  • 1
    Why do you need to escape single quotes? Those are fine in URLs. – Thilo May 20 '15 at 04:55
  • What do you need the encoding for at all? If you would tell us your actual use case (instead of just "*not url*"), we could suggest you an appropriate solution. If your only requirement is to "work like `escape`", then the solution is to use `escape`. – Bergi May 20 '15 at 05:10

1 Answers1

2

You could always build your own escape function, a-la:

function escapeHTML(html) {
    return html
         .replace(/&/g, "&")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#039;");
 }

There's also a terrible jQuery method, although it may not be cross-browser-safe and doesn't escape quotes or double quotes (which makes it kinda useless, to be honest):

var html = [INSERT SOME HTML HERE];
var escapedHTML = $("div.[SOME DIV]").text(html).html();

tl;dr: Use the first method and skip the second method unless you want semi-escaped strings.

grill
  • 1,160
  • 1
  • 11
  • 24