Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Javascript does not recognize character encoding after jquery loads a php

Posted on 2010-09-21
6
1,695 Views
Last Modified: 2013-12-13
Hi.
I have the following problem :

---- ccc.php -----
This file contains the correct header info to display greek.
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7" />
<script src="js/ccc.js" type="text/javascript"></script>




---- ccc.js -----
Inside this file if i write an alert('Greek characters here') everything will display correctly.
On a click event of a div located in ccc.php I load another php file called renew.php.
On the callback of the load procedure :
       
$('.just_a_div').load('./js/renew.php',function() {
            $.getScript('./js/renew.js');
       }
);


---- renew.php ----
header('Content-Type: text/html; charset=ISO-8859-7');


-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

Now the problem is that renew.js script... working for renew.php CANNOT display the ISO-8859-7 characters. Its as if it never gets the information from the headers. And display everything with black squares. (possibly utf-8 encoding)


I also tried to use ajax with contentType:
"application/x-www-form-urlencoded;charset=ISO-8859-7"
(solution from stack overflow)

but with no luck.

Thanks for your help.
0
Comment
Question by:CodedK
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33732209
This question came up before.  Your problem is that javascript uses UTF-8 internally.  AJAX uses javascript.  Javascript will corrupt anything but ASCII and UTF-8.  ASCII is a subset of UTF-8.  While your content header would work with the browser directly, it doesn't matter to javascript.
0
 
LVL 16

Author Comment

by:CodedK
ID: 33745407
Hi DaveBaldwin

Any solutions ? Or some trick to bypass this... To force js to act as it should?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33746217
Javascript uses Unicode internally which gets converted to UTF-8 in the browser and you can't change it.  Here's a discussion about it. http://stackoverflow.com/questions/919936/does-javascript-force-specific-charset  I found another article about 'escaping' characters that are not Unicode to make your text pass thru javascript without being screwed up.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Author Comment

by:CodedK
ID: 33746599
Thanks for the fast response.
So far i do use escape characters. This is uncomfortable.
Do you know any function to convert this characters from whatever language to an escape char string?

Example :

alert(CHANGE_TO_ESCAPE_CHARS(greek chars here))
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 33747023
Here's one article: http://www.zachleat.com/web/2010/03/26/charset/  Here's another: http://capitolacomputing.com/intl_js_charset.htm but I have to point out a problem.  It is one thing to have your page in a particular charset... but javascript internally will handle text as Unicode no matter what your page is using.  Loading text thru AJAX means that javascript reads and writes that text... in Unicode.

Here's a page about conversions: http://www.unicodetools.com/  and PSPad http://www.pspad.com/ will handle UTF-8.

The only way I found to make it work is to do everything in UTF-8.
0
 
LVL 16

Author Comment

by:CodedK
ID: 33755496
Thanks.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question