Questions tagged [encodeuricomponent]

encodeURIComponent is a core JavaScript function that escapes special characters in strings so that they can be safely used in URIs as components of query strings or hashes.

encodeURIComponent is a core JavaScript function, mainly used to escape special characters in strings that are going to be part of URI's query string. For example:

var string = "An `odd' string",
    enc_string = encodeURIComponent(string); // === "An%20%60odd'%20string"

The returned string can be safely used in URIs:

// makes an AJAX call to request.php?string=An%20%60odd'%20string
var xhr = new XMLHttpRequest();
xhr.open("GET", "request.php?string=" + enc_string, true);
xhr.onreadystatechange = callback;
xhr.send();

encodeURIComponent can be used to escape query strings' keys as well.

Differences with escape and encodeURI

encodeURIComponent escapes special characters to the format %##, where ## is the hexadecimal value of the special character's code, just like escape and encodeURI.

But escape does not escape characters like @, *, / and +, with the plus commonly interpreted as a space by web servers, so it shouldn't be used when possible. Moreover, escape encodes Unicode characters as %u####, while encodeURIComponent first converts them to the UTF8 character sequence, then it encodes the single bytes. For example:

var unichar = "\u25bc";               // A triangle pointing down
alert(escape(unichar));               // Alerts "%u25BC"
alert(encodeURIComponent(unichar));   // Alerts "%E2%96%BC"

encodeURI behaves similarly to encodeURIComponent but it doesn't encode some other character, such as /, & and =, as it should be used to escape whole URIs and not just parts of the query string.

Usage to convert a string to UTF-8 encoding

There's a nice trick to convert a string with Unicode characters to their corrisponding UTF-8 encoding:

function toUTF8(string) {
    return unescape(encodeURIComponent(string));
}

There's a way to convert it back from UTF-8:

function fromUTF8(string) {
    return decodeURIComponent(escape(string));
}
173 questions
0
votes
1 answer

apply encodeuricomponent to all ajax calls in an application globally

I have ajax calls like this at several places in my application. $.ajax({ type: 'POST', url: url, data: Json.stringify(Values), dataType: 'json' }); For these ones, I would like to add…
suk
  • 25
  • 8
0
votes
0 answers

$.get() HTML page with special character in page name

I'm trying to get through $.get() function the content of an HTML page. This page contains in its filename the special character +: mypage+v1.html Here is how I'm trying to get the content: $.get("mypage+v1.html", function(data){ …
BeNdErR
  • 17,471
  • 21
  • 72
  • 103
0
votes
1 answer

How to escape ':', "," and '()' under javascript array which supports both window and linux?

I have a javascript array with special characters are getting populated from back-end database. I wanted to escape those characters on the front-end to form query string to get the results properly. my array shown below: var a = new Array(); a[0] =…
Ramkumar
  • 446
  • 1
  • 14
  • 30
0
votes
1 answer

URL encoding to enocode spaces with plus

I have an application that needs to sign form data before it is sent to server. In the signing method, I have to escape all form input values with javascript, sign them and then send the signed data to server for cross matching. The remote server is…
Enn Fenn
  • 400
  • 2
  • 5
  • 15
0
votes
2 answers

string prototype custom method to make use of encodeURIComponent()

I am writing this method encodedURIComponentValue() for Javascript string: the idea is to allow me to call : "some string".encodedURIComponentValue() The code is as: if (typeof String.prototype.encodedURIComponentValue != 'function') { …
Dio Phung
  • 5,944
  • 5
  • 37
  • 55
0
votes
1 answer

Dotnet and JavaScript url encoding

The Following code returns http://localhost:23843/home/GetBooksTitles?partial=yes&typeofbook=livre instead of http://localhost:23843/home/GetBooksTitles?partial=yes&typeofbook=livre. It returns & instead of &. How do I tell dotnet not to…
Bellash
  • 7,560
  • 6
  • 53
  • 86
0
votes
1 answer

Need to quote a search string in javascript, then unquote it on the python backend

First, my code. HTML
Javascript, later in the file