Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery load() and national letters(like ę,ą,ć)

I'm using JQuery load() method to load content to page. The only problem is, when content is loaded via load() method all national(polish) chars are displaying invalid... On both pages loaded and main(in which content is load) coding is set to iso-8859-2(yeah, I know, I should use utf-8 bo it doesn't help in this case).

I don't really know how to solve it. The only solution I though of is replacing special chars with some code before loaded and after receiving data decoding it, but it's kind of complicated:D

Any ideas?

Cheers

like image 586
f1ames Avatar asked Jul 02 '11 09:07

f1ames


2 Answers

Ok, I did some research. And this is what I found:

jQuery .load() does not look into HTML meta tag for content-type. You can choose one of two options:

  1. To set HTTP response headers to Content-type: text/html; charset=iso-8859-2 and then use jQuery .load(). For instance in PHP you can do it by putting this at the top of the page:

    <?php header('Content-type: text/html; charset=iso-8859-2'); ?>

  2. To override HTTP response content type on client side using jQuery. For this purpose you should pass the setting mimeType: "text/html; charset=iso-8859-2" to $.ajax(). You can't do this using .load() because it does not support the ability to set ajax settings.

Both options tested, so everything should work! :)

like image 157
Karolis Avatar answered Sep 30 '22 02:09

Karolis


Assuming your chosen character set (ISO-8859-2) can actually represent the characters you want to use, it sounds like there's a problem with the file not being served from the server with the correct character set ('charset').

If you are using load() to request an HTML file, the charset parameter for HTML files can either be set by the Content-Type header in the response, or included as a meta tag in the HTML content.

Exactly how you set the Content-Type header depends on how you're generating or serving the HTML. The W3C has a good document that describes how to do it in several web servers and programming languages:

http://www.w3.org/International/O-HTTP-charset

Setting the charset meta tag might prove easier. The exact syntax differs between different versions of HTML, and you can find some information here:

http://en.wikipedia.org/wiki/Character_encodings_in_HTML#Specifying_the_document.27s_character_encoding

As several commenters suggested, if you want to maximise support for different languages in your website, it's also a good idea to consider moving towards a Unicode encoding like UTF-8, which minimises the chance of these incompatibilities occuring.

like image 32
Matt Ryall Avatar answered Sep 30 '22 03:09

Matt Ryall