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
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
  • 2
  • 12
4
votes
0 answers

encodeURIComponent behaves differently in browsers for china as location

i have an interesting problem in hand that involves chinese.. i use google custom search and append searched keywords as below.. window.location="http://search.xyz.com/search/cn/zh"+"#"+encodeURIComponent(searchedKeywords); results in chrome and…
4
votes
1 answer

Creating and downloading text file from string in JavaScript: Blob/createObjectURL vs. encodeURIComponent

As I've been looking for a way to create and download a text file from a website with JavaScript, I've found a bunch of solutions but generally using either Blob/createObjectURL or otherwise encodeURIComponent, with the former being more popular…
gaspar
  • 898
  • 1
  • 13
  • 26
4
votes
2 answers

dynamic image path not showing in [style.background-image] property in Angular 7

I am trying to use dynamic image path as background image in one of my Angular6 project. I have tried using [style.background-image] & [style.background] even encodeURI to fix spaces in path also tried using *ngIF so that it renders after getting…
Gaurav Janoti
  • 96
  • 1
  • 10
4
votes
2 answers

encodeURIComponent appears to add a character to my string

jQuery.ajax() is doing something weird when escaping my data. For example, if I send the request: $.ajax({ url: 'somethinguninteresting', data: { name: 'Ihave¬aweirdcharacter'; } }); then investigate the XHR in Chrome devtools,…
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
4
votes
2 answers

encodeURI file download - crashing browser

I created a web application to clean up CSV/TSV data. The app allows me to upload a CSV file, read it, fix data, and then download a new CSV file with the correct data. One challenge I have run into is downloading files with more than ~ 2500 lines.…
xited
  • 877
  • 1
  • 10
  • 19
4
votes
4 answers

Equivalent to Javascript's encodeURI?

C#'s equivalent to encodeURIComponent is well-covered on SO and elsewhere, but what about encodeURI? Basically I want to encode invalid URL characters only and not reserved characters such as /, :, etc. So "http://www.example.com/my cool…
Todd Menier
  • 37,557
  • 17
  • 150
  • 173
3
votes
1 answer

How do I apply encodeURIComponent() before submit?

I'm trying to apply encodeURIComponent() to the data before submitting it to the server. I've tried : submitdata: function (value, settings) { return { value: encodeURIComponent(value) } }, but this doesn't work. It always picks…
cowboyzed
  • 31
  • 4
3
votes
2 answers

How to properly handle UTF-8 in PHP?

I'm trying to get our chat system to support UTF-8, but I'm failing. If, on the client side, I send the following message, passed through encodeURIComponent: îûôó And put this on the PHP end: error_log(print_r(array( $_POST['message'], …
Aistina
  • 12,435
  • 13
  • 69
  • 89
3
votes
1 answer

Equivalent of Javascripts encodeURI in Powershell?

What is the equivalent of Javascripts encodeURI() / encodURIComponent() in Powershell? I'm encoding a URL (need some %20s in it) and I hate to do that manually.
leeand00
  • 25,510
  • 39
  • 140
  • 297
3
votes
2 answers

how do I properly encode a URL in JavaScript?

I am working on a browser plugin that takes the URL of the current page or any selected link as a parameter and send it to our server. When characters of the basic latin alphabet are present in the url (like http://en.wikipedia.org/wiki/Vehicle),…
Olivier
  • 305
  • 1
  • 4
  • 17
3
votes
1 answer

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

I'm looking for the equivalent, in MySQL, of the javascript function encodeURIComponent() (http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp) but can't find it. Anybody knows if such a function exist in MySQL?
woshitom
  • 4,811
  • 8
  • 38
  • 62
3
votes
1 answer

Encode special characters in cshtml

I have a razor view which has a hidden field Model.Token. The Token consist of special characters. This Token is appended to a link in href. Link If Model.Token has a +, the link renders it as a…
Jaseem Abbas
  • 5,028
  • 6
  • 48
  • 73
3
votes
2 answers

encodeURIComponent is really useful?

Something I still don't understand when performing an http-get request to the server is what the advantage is in using JS function encodeURIcomponent to encode each component of the http-get. Doing some tests I saw the server (using PHP) gets the…
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
3
votes
1 answer

How do I create and download a csv file using Javascript?

I have this code in a button: var csvContent = "data:text/csv;charset=utf-8,"; csvContent += myCSVcontent; var encodedUri = encodeURI(csvContent); window.open(encodedUri); This works perfectly in Chrome, Safari, and Firefox. - But not IE8 - I need…
1
2
3
11 12