6

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

f1ames
  • 1,620
  • 1
  • 18
  • 35
  • Maybe a meta language problem, like it's not in Polish? What browser did you test this in? –  Jul 02 '11 at 09:43
  • Are you sure **ą** and **ę** are polish letters? I always thought it's lithuanian only :) – Karolis Jul 02 '11 at 09:45
  • @Karolis Yeah, I'm sure, using them everyday:D Maybe lithuanian have same letters:D @KeokiZee I know I should... I'm using Opera. But I think the problem is connected to load() because when I open the same page in browser(not using load()) it works like a charm:D – f1ames Jul 02 '11 at 09:49
  • @f1ames Keep in mind that you not only have to add meta content-type tag in to the head of HTML, but also save these files in iso-8859-2 charset. – Karolis Jul 02 '11 at 10:01
  • It's a normal problem, characters are excepted to be entities on injection to the method. – Orbling Jul 02 '11 at 10:24
  • @Karolis Yes I know:) @Orbling Any known solutions? – f1ames Jul 02 '11 at 10:34
  • @Orbling hm...are you sure? I never had such a problem using `load` in my native language. – Karolis Jul 02 '11 at 11:11
  • @Karolis: Well, I am an English speaker, but I know it well enough from other symbols, like currency `£ £`, bullets `• •`, guillemets `« » « »`, etc. – Orbling Jul 02 '11 at 11:25

4 Answers4

5

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! :)

Karolis
  • 8,967
  • 26
  • 38
1

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.

Matt Ryall
  • 8,532
  • 5
  • 21
  • 19
  • Thx for response:) I have meta tag in my document and it works well. When I open page in browser, chars are displayed correctly but when I get the same page via load method chars are just displaying invalid ... – f1ames Jul 02 '11 at 12:34
  • Sounds like my first suggestion, setting the charset in the Content-Type header actually worked out. Oh well, good to know. – Matt Ryall Jul 10 '11 at 01:32
  • Ohhh, I voted up. @Karolis answer was just clear answer, he actually tested and solved the problem. Your answer was correct too, but as you said it was "suggestion". I think that's why I choose his answer. Of course I also appreciate your help and effort:) – f1ames Jul 22 '11 at 09:31
1

Is it all polish? If not, you could try HTML entities for those characters, browser will do the decoding.

http://en.wikipedia.org/wiki/Polish_alphabet#Computer_encoding

Murali VP
  • 5,549
  • 4
  • 25
  • 35
0

I have had the same problem and i solved it by reading multiple threads. What i did was create a new function/plugin and add the mimetype and contentType in it and it returned the correct encoding.

    (function($){
    $.fn.formatload = function( url, params, callback ) {
        if ( typeof url !== "string" ) {
            return _load.call( this, url );

        // Don't do a request if no elements are being requested
        } else if ( !this.length ) {
            return this;
        }

        var off = url.indexOf(" ");
        if ( off >= 0 ) {
            var selector = url.slice(off, url.length);
            url = url.slice(0, off);
        }

        // Default to a GET request
        var type = "GET";

        // If the second parameter was provided
        if ( params ) {
            // If it's a function
            if ( jQuery.isFunction( params ) ) {
                // We assume that it's the callback
                callback = params;
                params = null;

            // Otherwise, build a param string
            } else if ( typeof params === "object" ) {
                params = jQuery.param( params, jQuery.ajaxSettings.traditional );
                type = "POST";
            }
        }

        var self = this;

        // Request the remote document
        jQuery.ajax({
            url: url,
            type: type,
            mimeType: "text/html; charset=iso-8859-2",
            dataType: "html",
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            data: params,
            complete: function( res, status ) {
                // If successful, inject the HTML into all the matched elements
                if ( status === "success" || status === "notmodified" ) {
                    // See if a selector was specified
                    self.html( selector ?
                        // Create a dummy div to hold the results
                        jQuery("<div />")
                            // inject the contents of the document in, removing the scripts
                            // to avoid any 'Permission Denied' errors in IE
                            .append(res.responseText.replace(rscript, ""))

                            // Locate the specified elements
                            .find(selector) :

                        // If not, just inject the full result
                        res.responseText );
                }

                if ( callback ) {
                    self.each( callback, [res.responseText, status, res] );
                }
            }
        });

        return this;
    }
})(jQuery);

It is called as a normal jQuery load. e.g

$('#div').formatload(url, data, function(data){/*do stuff here */ });

  • Better solution, just use: header("Content-type: text/html; charset=utf-8"); and your mysqli connect like... @ $db = new mysqli('localhost', 'login', 'password', 'database'); mysqli_set_charset($db, 'utf8'); $query = "select * from books where ".$searchtype." like '%".$searchterm."%'"; http://stackoverflow.com/questions/3811257/problems-in-inserting-utf-8-string-into-database-and-then-outputting-it-to-web-p – Jehu Barça Cummings May 29 '13 at 05:25