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));
}
163 questions
139
votes
4 answers

How to encode URL parameters?

I am trying to pass parameters to a URL which looks like this: http://www.foobar.com/foo?imageurl= And I want to pass the parameters such as an image URL which is generated itself by another API, and the link for the image turns out…
Apoorv Saxena
  • 3,736
  • 9
  • 27
  • 44
95
votes
5 answers

What is the equivalent of JavaScript's encodeURIcomponent in PHP?

What is the equivalent of JavaScript's encodeURIcomponent function in PHP?
Gal
  • 20,759
  • 30
  • 92
  • 114
27
votes
3 answers

url-Encode vs Base64 encoding ( usages)?

I was wondering... (except the issue with the base64's plus'+' sign in query string - which is translated to 'space' and can be solved by %2b) :---> which is the preferred way to transfer data in query string? Both functions can be used through the…
Royi Namir
  • 131,490
  • 121
  • 408
  • 714
18
votes
2 answers

javascript encodeURIComponent and converting spaces to + symbols

I would like to encode my URL, but I want to convert spaces to plus symbols. This is what I attempted to do... var search = "Testing this here &"; encodeURIComponent(search.replace(/ /gi,"+")); The output from that is Testing%2Bthis%2Bhere%2B%26…
Ian
  • 1,740
  • 6
  • 21
  • 37
17
votes
5 answers

Firefox automatically decoding encoded parameter in url, does not happen in IE

I am having frustration between Firefox and IE, well mostly Firefox as it is automatically decoding a parameter in the hash before I can work with it in Javascript. IE does not automatically decode the url thus not giving me reading errors. My…
Alex
  • 363
  • 2
  • 7
11
votes
1 answer

Unable to download large data using javascript

I have a large data in form of JSON object in the javascript. I have converted it into the string using JSON.stringify(). Now my use case is to provide this large string in a text file to the user. So for this i have written below code. HTML code …
ankur37
  • 111
  • 2
  • 5
9
votes
2 answers

Different behaviour of encodeURIComponent() in Firefox only

I encode a filename and send it as a part of URL like /rest/get?name=Filename.txt. In JS link construction is as simple as url = '/rest/get?name=' + window.encodeURIComponent(file.name); It works good for simple cases but for hardcore testing I use…
naXa
  • 26,677
  • 15
  • 154
  • 213
8
votes
2 answers

jQuery encodeURI for href not working

I'm having problems encoding a string so I can place a variable into a link. I'm sure this is really simple, but i had trouble turning anything up. $("a.inquiry").attr("href", "/inquiry/6933/text=" + encodeURI("text o")); This doesn't…
holden
  • 13,081
  • 21
  • 89
  • 157
6
votes
3 answers

Lua - decodeURI (luvit)

I would like to use decodeURI or decodeURIComponent as in JavaScript in my Lua (Luvit) project. JavaScript: decodeURI('%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82') // result:…
Kosmetika
  • 19,249
  • 37
  • 94
  • 154
5
votes
3 answers

how can I javascript decodeURI in PHP?

I have a javascript which sends some specific information to a PHP api . Before to send it performs encodeURI . How can I "decode" it in PHP ? I understand that urldecode/urlencode is different that javascript encode/decodeURI so what can I use ?
Michael
  • 6,149
  • 14
  • 54
  • 91
5
votes
2 answers

Twitter Share encodeURIcomponent Struggle

I'm working on a project and I'm having some trouble with the encodeURIcomponent function. The site is a social media content bank that allows me to store copy in a CMS and display it on it's on page with links and buttons to share to social media…
5
votes
2 answers

Trouble encoding a u umlaut with in a .Net http handler

I have a JavaScript request going to a ASP.Net (2.0) HTTP handler which passes the request to a java web service. In this system special characters, such as those with an accent do not get passed on correctly. E.G. Human input: Düsseldorf becomes a…
dlamblin
  • 40,676
  • 19
  • 92
  • 127
5
votes
1 answer

Javascript charAt() breaking multibyte character string

This code breaks with nodejs v0.10.21 #!/usr/bin/env node "use strict"; var urlEncoded = 'http://zh.wikipedia.org/wiki/%F0%A8%A8%8F'; var urlDecoded = decodeURI( urlEncoded ); var urlLeafEncoded = urlEncoded.substr( 29 ); var urlLeafDecoded =…
5
votes
1 answer

empty string + null = "null"?

I came across an odd occurrence today while troubleshooting a rogue AJAX request in our application. We send data back to the server, using jQuery.param to build the request string. In one scenario, the object fed to the param function had a null…
Thomas Jones
  • 4,702
  • 22
  • 32
4
votes
0 answers

Setting filename using encodeURIComponent() to let user download data file

Alright, so I have this webpage page that gets its data using AJAX requests (in javascript using JQuery). Once the data has been loaded on to the users browser I want to let them download said data without having to request it from the server…
DJ SymBiotiX
  • 187
  • 1
  • 10
1
2 3
10 11