0

I'm kinda new to javascript running on Apache but I have the following problem.

I use escape to make the string "Café" ready for usage in an url using javascript.

"&place=" + escape(placename);

The String is translated to "Caf%E9" and then represented as "Café" in the next page, which is a php page.

All is fine, however, when I upload the code to my webhost it is translated to "Caf%C3%A9". And shown in the page as "Café".

I use UTF-8 for the decoding. My webhost uses iconv.input_encoding ISO-8859-1.

I think here lies the problem. What would be the best way to fix this? Change my UTF-8 to ISO-8859-1 or is there some converter function ?

EDIT I have the UTF8 meta tags

<meta http-equiv="content-type" content="text/html; charset=utf-8"/>

I also tried to use encodeURIComponent but it gives the same result

"&place=" + encodeURIComponent(placename);

EDIT: added code

This is the code, it behaves differently locally than on the web. The code uses the Google Maps API.

    var lat = place.geometry.location.lat();
var lng = place.geometry.location.lng();
var placename = place.name;
var placerating = place.rating;
if (placerating == undefined) {
    placerating = 0;
}
var placeaddress = place.vicinity;
var website = place.website;
var url = place.url;

var day = new Date();
var m = day.getMonth() + 1;
var d = day.getDate();
var y = day.getFullYear();

var formUrl = "myform.php?d=" + d + "&m=" + m + "&y=" + y + "&lat=" + lat + "&lng=" + lng + "&place=" + encodeURIComponent(placename);

Note, when I alert(placename) it gives me the same result on both environments. namely, "Café"

SOLUTION

My webhost used another encoding which forces the page to use ISO-8859-1. Even though I used the html meta tags.

add the following PHP header to the beginning of the pages

header("Content-type: text/html; charset=utf-8");

TIA, Coen

Coen Damen
  • 2,009
  • 5
  • 29
  • 51

2 Answers2

1

escape isn't for escaping query string parameters, that's what encodeURIComponent is for. escape does something similar, but different, and so it's not 100% compatible with the server half of the equation.

Separately, though, you can't encode text with one encoding and have it successfully decoded at the other end using a different encoding. You should be able to put the page in UTF-8 rather than ISO-8859-1, through apache headers. If you can't do it in headers, you can do it in the HTML: At the top of the head:

<meta charset="UTF-8">

...and then, of course, ensure that the file really is encoded in UTF-8 (e.g., check your editor settings, etc.).

This other answer here on StackOverflow may be useful as well, it talks about encodings as they relate to URIs.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Now, I was going to type that :p [`encodeURIComponent`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent) – Rob W Jun 20 '12 at 13:04
  • Hi TJ, well I have tried that and I do use the meta tags but it makes no difference. strange right? – Coen Damen Jun 20 '12 at 13:19
  • Hi TJ, I think the pages are fine, I would just like to know why the String Café is transformed differently in JavaScript on my webhost than locally on my dev machine. when I 'alert' the encodeURIComponent(placename) it gives (on browser client side) two different results (??) – Coen Damen Jun 20 '12 at 13:43
  • I've found it already, on my webhost the encoding is forced to Latin unicode U+00E9 é == c3 a9 LATIN SMALL LETTER E WITH ACUTE. Even though I have the meta tags set to UTF-8 it is still encoded to iso-8859-1. How can I bypass this ? – Coen Damen Jun 20 '12 at 14:07
  • Hi TJ, my webhost has a default character set of ISO-8859-1, locally I use the Apache config of "AddDefaultCharset UTF-8". So there is the mismatch. When in FF I change the encodig to UTF-8 the page works fine. So there is nothing wrong with my code but the problem lies with the configuration of my website. Thanks for your help in pointing me to the right direction. – Coen Damen Jun 20 '12 at 15:22
  • @CoenDamen: I'm not at all sure there isn't a code-based solution. Why not spend the 10 minutes? I'm completely failing to understand the reluctance here, but this is the last I'll bring it up. – T.J. Crowder Jun 20 '12 at 15:25
  • @CoenDamen: **Congrats!** Glad you found it. It's perfectly fine to [answer your own questions here on Stack Overflow](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/). The correct way is to leave the question as a question, and post an actual answer. After two days, you can accept your own answer, which marks the question "answered", etc. Let's also clean up our discussion above. Congrats again! – T.J. Crowder Jun 20 '12 at 15:49
0

SOLUTION

My webhost used another encoding which forces the page to use ISO-8859-1. Even though I used the html meta tags.

add the following PHP header to the beginning of the pages

header("Content-type: text/html; charset=utf-8"); 

Alternatively, I asked my webhost to change the encoding to UTF-8, which they did.

Coen Damen
  • 2,009
  • 5
  • 29
  • 51