Solved

Javascript does not recognize character encoding after jquery loads a php

Posted on 2010-09-21
6
1,752 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
[X]
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
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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