Solved

Javascript does not recognize character encoding after jquery loads a php

Posted on 2010-09-21
6
1,651 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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
Hi DaveBaldwin

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

Expert Comment

by:Dave Baldwin
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 16

Author Comment

by:CodedK
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now