PDA

View Full Version : Dynamic Ajax Content charset problem



stormrider121
07-02-2006, 06:50 PM
Dynamic Ajax Content
http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm

When my external page includes characters like "ü,ö,ı", it seems like "?" (My external page has a meta tag "charset=windows-1254" in head section).

When i search forum i see that



I'm sure it should, but I'm afraid that's just not possible. A <meta> tag, not the best of ideas anyway, needs to go in the head; this AJAX script won't necessarily be able to write to the head.


is there nothing to avoid this character problem?

jscheuer1
07-03-2006, 08:12 AM
Are you using named or number entities or the actual characters?

The meta tags and anything else in the head section of the external page are lost with this script. You can load javascripts and/or css to the 'top' page for use with the external content using loadobjs(). If you need a meta tag for content on the external page, you could simply put it in the head on the 'top' page.

stormrider121
07-03-2006, 12:06 PM
Thank you for your reply.

My top page has a meta tag like "charset=windows-1254" and also external page has same meta tag.

I am using actual characters.I want to show rss feeds with this dynamic content.

if it will avoid the problem i can replace characters with their named or number entities.(Edit:But i could not found the number entities of the characters like"ı,ğ,ş")

jscheuer1
07-03-2006, 03:47 PM
Any good ASCII to hex converter:

http://centricle.com/tools/ascii-hex/

will give you the hex value for a character, the above one precedes it with a % sign. Once you have the number, you can make a number entity for it. In the case of ğ it has a hex value of 11f, so your entity would be - &#x11f;

This may or may not solve your problem though, worth a shot.

stormrider121
07-11-2006, 08:32 AM
This didn't solve the problem.

I have same problem with another Ajax script. Can we add a code to AJAX to write head part of external page?Please help.


Ajax Tabs Content script

http://www.dynamicdrive.com/dynamicindex17/ajaxtabscontent/index.htm

Dammy
07-11-2006, 01:51 PM
If you'r using an external .php, use
header('content-type: text/html; charset=YOURCHARSET');
in the php-file.

jscheuer1
07-11-2006, 03:06 PM
I did a simple test here and it worked:

http://home.comcast.net/~jscheuer1/side/ajax_h.htm

Here are my two files:

ajax_h.htm:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

/***********************************************
* Dynamic Ajax Content- Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

</script>
</head>
<body>
<a href="javascript:ajaxpage('hex_h.htm', 'contentarea');">test</a>
<div id="contentarea"></div>
</body>
</html>

and hex_h.htm:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>
&#x11f;
</body>
</html>

apostolidis
02-18-2007, 03:48 PM
I have the exact same problem. Cannot display greek characters in the external php file. In each page i have charset=utf-8 and all characters are being displayed right until i use the above script. In firefox and opera everything seems right, but in ie i see ?????????? in place of the greek word. I have this problem using either named or number entities of the characters. Adding header('content-type: text/html; charset=utf-8); to the php file didn't help. Any other ideas?

jscheuer1
02-18-2007, 05:07 PM
If using named entities isn't taking care of it, see:

http://www.dynamicdrive.com/forums/showpost.php?p=45178&postcount=14

and the thread linked to it.

apostolidis
02-18-2007, 05:46 PM
I added AddDefaultCharset utf-8 to .htaccess file. I tested it locally (with wamp installed) and on my server. It still won't work in ie. Is there a way to modify the script to get this done? If not, any other ideas (or scripts)?

jscheuer1
02-18-2007, 08:27 PM
Using wamp might not be the best way to test .htaccess effects. Try it live before giving up on that method. Make sure your .htaccess file is having some effect on other things by devising a test to see if the server is using it or not. The file often must be in the directory where the files that use are located. You can have one in each directory that needs it and the including it in the directory for both external files and the top page can't hurt.

mfgarz
01-09-2008, 01:02 PM
Hi people, I'd faced the very same problem while developing a Java application.
The whole application is using ISO-8859-1 while AJAX uses UTF-8.
I soved my problems by adding a ServletRequestWrapper to the request.
First I extended the ServletRequestWrapper class, and then I overwrote the method "String getParameter(String)" to return a UTF-8 Converted string.
--
String tmp = super.getParameter(arg0);
if ( tmp != null )
return new String ( tmp.getBytes() , "UTF-8");
else
return tmp;

---

Later I added a filter that wraps every request in my new class...

req = new AjaxUTFRequestWrapper((HttpServletRequest)req)


So this solved my problem.

Gonegold
01-09-2008, 02:03 PM
mfgarz, great solution.
But can you put here final text of scripts and html code?
For copy-paste using.
Thanks!

jscheuer1
01-09-2008, 03:42 PM
Hi people, I'd faced the very same problem while developing a Java application.
The whole application is using ISO-8859-1 while AJAX uses UTF-8.
I soved my problems by adding a ServletRequestWrapper to the request.
First I extended the ServletRequestWrapper class, and then I overwrote the method "String getParameter(String)" to return a UTF-8 Converted string.
--
String tmp = super.getParameter(arg0);
if ( tmp != null )
return new String ( tmp.getBytes() , "UTF-8");
else
return tmp;

---

Later I added a filter that wraps every request in my new class...

req = new AjaxUTFRequestWrapper((HttpServletRequest)req)


So this solved my problem.


mfgarz, great solution.
But can you put here final text of scripts and html code?
For copy-paste using.
Thanks!

Java and javascript (although they share a bit in the language and syntax departments) are two quite different things. If your problem is with Ajax and javascript, you would be better off using the character entity or working on the server side.

jeffsalam
02-26-2008, 07:37 PM
hi mfgarz;

I'm new to this .can you post the script so I can make the changes to my script .


thanks

regisnox
03-26-2010, 11:57 AM
I have exactly the same problem and i tryed everything
but that solution dont work completlty.
Can you tell me where i should paste that or what i should add to change that charset problem.

pmachado
04-15-2011, 12:30 PM
Ajax content don't come with charset and the server overrides the page header character set as the Apache server uses the default charset which usually is UTF-8. I did change all dbase tables text collations to latin1_swedish_ci to make sure the special characters are saved well and added to my public_html .htaccess the line:

AddDefaultCharset iso-8859-1

UTF-8 uses html entities wich causes problems for clients copying and pasting from several sources. iso-8859-1 with latin1_swedish_ci collations solved the problem for me.