Loading jQuery () and national letters (e.g. ę, ±, ć)

I am using the jQuery () load method to load content into a page. The only problem is that when the content is loaded using the load () method, all national (polishing) characters are not displayed correctly ... On both loaded pages and mainly (in which the content is loaded) the encoding is set to iso-8859- 2 (yes, I know, I should use utf-8 bo, this will not help in this case).

I really don't know how to solve it. The only solution I want to make is to replace the special characters with some code before downloading and after receiving the data decoding it, but this is rather complicated: D

Any ideas?

Greetings

+6
source share
4 answers

Well, I did some research. And here is what I found:

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

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

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

  • To override the content type of the HTTP response on the client side using jQuery. To do this, you must pass the mimeType: "text/html; charset=iso-8859-2" setting mimeType: "text/html; charset=iso-8859-2" to $.ajax() . You cannot do this with .load() because it does not support the ability to set ajax parameters.

Both options are tested, so everything should work! :)

+5
source

Assuming that your selected character set (ISO-8859-2) may actually represent the characters you want to use, it looks like there is a problem with a file that is not served from the server with the correct character set ("charset").

If you use load() to request an HTML file, the charset parameter for the HTML files can be specified 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 create or maintain the HTML. W3C has a good document describing how to do this on several web servers and programming languages:

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

Setting up a meta tag can be simpler. The exact syntax differs from different versions of HTML, and you can find the following information here:

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

As suggested by several commentators, if you want to maximize support for different languages ​​on your website, it's also a good idea to consider switching to Unicode encoding such as UTF-8, which minimizes the chance of these incompatibilities.

+1
source

Is it all Polish? If not, you can try HTML objects for these characters, the browser will decode.

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

+1
source

I had the same problem and solved it by reading several threads. What I did was create a new function / plugin and add the mimetype and contentType types to 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 a function if ( jQuery.isFunction( params ) ) { // We assume that it 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 regular jQuery download. eg

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

0
source

Source: https://habr.com/ru/post/891864/


All Articles