-1

I have this code:
html code:

<script>
function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
var xmlDoc = loadXMLDoc('users.xml');
var x = xmlDoc.getElementsByTagName('usr');
document.write(x.length);
</script>

xml code:

<?xml version="1.0" encoding="utf-8"?>
<users xmlns="http://localhost" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost users.xsd">
<usr><age>25</age><country>الاردن</country></usr>
<usr><age>30</age><country>مصر</country></usr>
</users>

And I want to include the html code in another html document like this:
html code:

<!DOCTYPE html unicode="utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
include 'users.html';
?>
</body>
</html>

the output of the javascript code is: 0 while it should be 2 and if I replace the arabic text in the xml document with english text the output become 2.
Why is the arabic text breaking the code?

user3926604
  • 179
  • 2
  • 12
  • Cannot reproduce. Can you show the relevant HTTP headers and/or give a live URL for testing? Are there any error messages in the browser console? – Jukka K. Korpela Aug 28 '14 at 12:26
  • @JukkaK.Korpela this is what I get when running var_dump(headers_list()); "array(2) { [0]=> string(24) "X-Powered-By: PHP/5.3.24" [1]=> string(23) "Content-type: text/html" }" and no I don't have a live url and there is no error messages in the browser console. – user3926604 Aug 28 '14 at 12:34
  • What happens if you insert the `script` element directly into the html code instead of using PHP for the inclusion? (I’m just trying to reduce the problem to a simpler case.) – Jukka K. Korpela Aug 28 '14 at 12:40
  • @JukkaK.Korpela same problem. – user3926604 Aug 28 '14 at 12:45
  • My test, with the code as in the question but with direct inclusion of HTML code rather than with PHP: http://www.cs.tut.fi/~jkorpela/test/users.html8 Works on IE, Chrome, Firefox. – Jukka K. Korpela Aug 28 '14 at 13:02

1 Answers1

0

I'm not shure why you want to include the HTML('users.html') which is not described in your question inside a php page if your using ajax. anyway i see some minor problems in your code.

the correct formatting for utf8 in modern browsers is this:

<!doctype html>
<html>
<head>
<meta charset="utf-8"><!-- how to set utf8 in modern browsers -->
<title>Users</title>
<script>
function show(){
 document.body.innerHTML=this.responseXML.getElementsByTagName('usr').length
}
window.onload=function(){
 with(new XMLHttpRequest)open('get','users.xml'),onload=show,send()
}
</script>
</head>
<body>
</body>
</html>

this example works(using your xml with arabic language) on ie 10,chrome,safari,ios,android,opera e probably also in firefox.

now if you totally need to include the html inside a php page you need to check some php settings maybe.... with a fast search i found this ...

mb_internal_encoding("UTF-8");

http://php.net/manual/en/function.mb-internal-encoding.php

but there are many ways to set the encoding to utf8 & include files...

Personally i wouldn't use your approach if you use ajax... if you insist post your users.html so that we have more information about what can cause the problem.

the js code returns properly the length of 2 and the ajax is async, how it should be. more about ajax: https://stackoverflow.com/a/18309057/2450730

the reason i used with: The use of "with" statement in javascript, in some extreme rare cases

note:including a whole html page inside another html page is not a correct way to do, you should only include necessary parts (html elements).

if you have any questions about my code just ask.

Community
  • 1
  • 1
cocco
  • 16,442
  • 7
  • 62
  • 77