6

I'm using the following code to load in content:

<script type="text/javascript"> jQuery.noConflict(); jQuery(document).ready(function(){ jQuery('.content-div').load("all-events.html"); }); </script> 

However the content is loaded in without the special characters that I want in UTF-8. It was mentioned somewhere to put this at the top of my code:

$html = header('Content-Type: text/html; charset=utf-8'); 

However I only got this error: 'header is not defined'.

4
  • Can you give us an example? It's pretty hard to guess without seeing real data. Commented Oct 28, 2011 at 1:36
  • Sorry about that! This is the Main page and this is where the content is loaded from Commented Oct 28, 2011 at 1:43
  • Your second code block with the call to header() is PHP? What are you using to serve your pages. The code snippet you're trying to implement is suggesting that the server-side HTTP response to your client-side load() needs to include a content-type header. Commented Oct 28, 2011 at 3:15
  • Okay, so if you're talking about PHP I used the following code: <?php header ('Content-type: text/html; charset=utf-8'); ?> which gave me this error Warning: Cannot modify header information - headers already sent by (output started at Commented Oct 28, 2011 at 11:25

3 Answers 3

10

Thanks for the help everyone, however I found what was the problem. The problem was that adding headers to a basic text file didn't change its actual encoding. I wrote that file in notepad and had no way of checking if the encoding was correct. I then opened it in Notepad++ and set the encoding from that application. Hope that helps to others struggling with this problem :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I set 'UTF-8 without BOM' encoding in Notepad++ and it works! :)
6

I was experiencing the same problem, but the original poster's accepted answer didn't help. Doing one of the bellow actions solved it for me:

  1. Changing the retrieved document's extension from ".html" to ".php".

  2. Adding a .htaccess file to the main directory of the home page with the following content:

    AddDefaultCharset UTF-8 
  3. A combination of the two above


More about this problem:

Examining response and request headers with Firebug, it was possible to see the server (Apache) was returning content encoded in charset ISO-8859-1. What I needed was UTF-8.

Although all files were encoded as UTF-8 without BOM and

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

was being called on the main .php file, the dynamically included .html file had its elements filtered out because jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document, as noted by Mike Haboustak. See jQuery's documentation for load for more details.

1 Comment

.htaccess worked for me. Thank you! Was trying to fix it for quite some time
1

jQuery's load() method uses the browser's innerHTML implementation to load the html returned from the request into the current document. This means that the incoming html has to use the same text encoding as the current document. You can't insert UTF-8 characters into an ISO 8859-1 document.

You need to fix this at the server, not in Javascript. The server-side code that is responding with your original Html (that contains your jQuery) needs to include a content-type header with the right character encoding. If you're using Php, I think you've found an example of how to do that.

2 Comments

Okay, so if you're talking about PHP I used the following code: <?php header ('Content-type: text/html; charset=utf-8'); ?> which gave me this error Warning: Cannot modify header information - headers already sent by (output started at
It needs to be the first line of your PHP file, before any content gets sent (even whitespace). See the PHP docs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.