I’m having an issue reading the below XML document in Chrome – nothing seems to turn up but it works perfectly fine in Firefox. Is it a browser compatibility issue?
<html>
<head>
<title>XML</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type='text/javascript'>
function XMLload(){
jQuery.post(url,function(data){getxml(data);},'xml');
}
function dataFromTag(node,t){
var d=node.getElementsByTagName(t);
if(d.length==0)
return('');
return(d[0].firstChild.nodeValue);
}
jQuery(document).ready(function(){XMLload();});
// all the code above this line can be re-used without change
// all the code below will need altering for a different document and different structure
var url='cohort.xml';
var xmlcohort;
function getxml(xmldoc){
xmlcohort = xmldoc.getElementsByTagName('student');
var hstr = '<html><head><title></title></head><body>';
hstr += '<p>Cohort:</p><form>';
hstr += '<select size="' + xmlcohort.length + '" onclick="parent.student_info(this.selectedIndex);">';
for( var i = 0; i < xmlcohort.length; i++ ) {
hstr += '<option>' + dataFromTag(xmlcohort[i], 'name') + '</option>';
}
hstr += '</select></form></body></html>';
with( document.getElementById('cohort').contentDocument ) {
open();
write(hstr);
close();
}
}
function student_info(idx) {
var hstr = '<html><head><title></title></head><body><dl>';
hstr += '<dt>Name</dt><dd>' + dataFromTag(xmlcohort[idx], 'name') + '</dd>';
hstr += '<dt>Module</dt><dd>' + dataFromTag(xmlcohort[idx], 'module') + '</dd>';
hstr += '<dt>Mark</dt><dd>' + dataFromTag(xmlcohort[idx], 'mark') + '</dd>';
hstr += '</dl></body></html>';
with( document.getElementById('student').contentDocument ) {
open();
write(hstr);
close();
}
}
</script>
</head>
<body>
<iframe id='cohort' height='300' width='200'></iframe>
<iframe id='student' height='300' width='200'></iframe>
<p>Here is the <a href="cohort.xml">xml data</a></p>
</body>
</html>
Here are the contents of cohort.xml:
<cohort>
<student>
<name>Bob</name>
<module>COA122</module>
<mark>72</mark>
</student>
<student>
<name>Alice</name>
<module>COB250</module>
<mark>84</mark>
</student>
<student>
<name>Kate</name>
<module>COP180</module>
<mark>99</mark>
</student>
</cohort>
I get this error when I run it from my hard drive. The entire page doesn’t load – it only works when I put it on a web server:
XMLHttpRequest cannot load file:///C:/Users/methuselah/Desktop/cohort/cohort.xml. Origin null is not allowed by Access-Control-Allow-Origin.
This is a known issue in Chrome: http://code.google.com/p/chromium/issues/detail?id=40787
If this is just for testing and development, there is a Chrome command line flag you can use to work around this:
--allow-file-access-from-files. I just tested it and it makes your pages work locally in Chrome for me.Windows:
chrome.exe --allow-file-access-from-filesMac:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-filesLinux: Linux users don’t need instructions for how to get things working from the command-line! 😉