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
3
votes
4 answers

∞ gets converted to ∞ when inserted into MySQL table

The infinity (∞) symbol gets converted to ∞ when it is inserted into my MySQL table by a PHP script, but if I insert it directly from phpMyAdmin, it inserts correctly. The symbol is passed to the PHP script by a GET call with the JavaScript…
James Simpson
  • 13,488
  • 26
  • 83
  • 108
3
votes
1 answer

how to export csv file with filename

I want to export the exist data into csv file. I try to use this code: var uriContent = "data:text/csv;charset=utf-8," + encodeURIComponent(data); var myWindow = window.open(uriContent); myWindow.focus(); it works but I can design filename. I can…
user1827864
  • 33
  • 1
  • 1
  • 4
3
votes
2 answers

What is the correct way to encodeURIcomponent non utf-8 characters and decodes them accordingly?

I have a Javascript bookmarklet that uses encodeURIcomponent to pass the URL of the current page to the server side, and then use urldecode on the server side to get the characters back. The problem is, when the encoded character is not in utf-8…
lazycai
  • 31
  • 1
  • 3
2
votes
3 answers

Laravel - Proper way to support a URL piece that might have a slash in it?

I've defined a route in web.php that looks like this: Route::delete('/app/charges-for-order/{orderId}', 'AppController@deleteOrderCharges'); It seems to work well apart from when orderId has a forward slash in it. Eg 11/11. Naturally my first port…
TKoL
  • 13,158
  • 3
  • 39
  • 73
2
votes
1 answer

Encoding GBK2312 Condundrum

I am an amateur coder and I have a small problem. My goal is to have one text input with two buttons. The first button uses a bit of Javascript called SundayMorning to translate the text (to Chinese) The second button submits the text to a URL.…
user792271
  • 21
  • 1
2
votes
1 answer

can someone tell me the why we need decodeURIComponent

I have this piece of code that I couldn't find any explanation for. When I googled decodeURIComponent it said it is the reverse of encodeURIComponent, however, I can't find encodeURIComponent anywhere in my code. getParameterByName = (name, url) =>…
JS Lover
  • 622
  • 5
  • 16
2
votes
3 answers

javascript - creating URI string from objects

I have the following variables in javascript var domain= http://example.com/api/v1/purchases/get_purchases.json? var header = 'df0a96ddbe1ada9fda4b1ed9b02cf67c' var params = { search:{ status_eq:…
subha
  • 426
  • 1
  • 3
  • 14
2
votes
2 answers

encodeURIComponent in elixir

looking for elixir way to encode a uri component ie javascript encodeURI("&") "&" encodeURIComponent("&") "%26" Elixir URI.encode("&") "&" pry(11)> URI.encode_query(%{k: " & "}) "+k=%26+" basically I want encode_query but not have to do key value…
crododile
  • 145
  • 12
2
votes
0 answers

javascript encode uri and arrays/objects

I'm trying to write a function that will run encodeURIComponent on arrays or objects regardless of the depth. It all seems to go well until I return the final result.