V
VK
In continuation of http://groups.google.com/group/comp.lang.javascript/msg/787389d10afcaf77
Test (unzip and run AJAX/test/index.html):
http://sites.google.com/site/schoolsring/javascript
LocalDataReadingTest.zip
In each test:
1) attempt to read xml file in the same directory
2) attempt to read xml file in subdirectory
3) attempt to read xml file in top directory
4) attempt to read xml file in sibling directory
Each reading made twice: one time with without overriding MIME type,
next time with implied "text/xml" type.
1) IE
Internet Explorer 8.0 / Windows Vista SP2 - newest
Internet Explorer 6.0.2900 / Windows XP SP2 - legacy test
window.XMLHttpRequest in new IE does NOT allow local data reading at
all.
window.ActiveXObject(ProgID) doesn't support .overrideMimeType method
Without implied "text/xml" Content-type the results are:
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
OK attempt to read xml file in top directory
OK attempt to read xml file in sibling directory
success request status = 0
.requestXML is empty, a manual serialization from responseText is
needed later.
2) Fx
Mozilla Firefox 3.6.3 / Windows Vista SP2 - newest
Mozilla Firefox 3.5.7 / Windows XP SP2 - legacy test
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
_ attempt to read xml file in top directory (Error: Access to
restricted URI denied)
_ attempt to read xml file in sibling directory (Error: Access to
restricted URI denied)
success request status = 0
.requestXML is filled properly either with or without
using .overrideMimeType method
3) Sf
Apple Safari 4.0.5 / Windows Vista SP2
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
OK attempt to read xml file in top directory
OK attempt to read xml file in sibling directory
success request status = 0
.requestXML is filled properly either with or without
using .overrideMimeType method
4) Ch
Google Chrome 4.1.249 / Windows Vista SP2
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
OK attempt to read xml file in top directory
OK attempt to read xml file in sibling directory
success request status = 0
.requestXML is filled properly either with or without
using .overrideMimeType method
5) Op
Opera 10.51 / Windows Vista SP2
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
OK attempt to read xml file in top directory
OK attempt to read xml file in sibling directory
success request status = 0
.requestXML is filled properly either with or without
using .overrideMimeType method
Conclusions:
1) IE sucks but usable
2) Fx is the most strict
3) overrideMimeType is useless: it is not needed where supported
and it is not supported where needed
4) The quality of XMLHttpRequest instantiation blocks in all
prominent libraries is *awful*... No, sorry: it is AWFUL. No... It is
a BLOODY AWFUL NIGHTMARE. The guys didn't read MSDN for years. If they
don't care about IE users whatsoever is fine, but they should
explicitly mark it then. I am posting the XHR init part from the test
file here - not as a sample of an outstanding coding, but at least to
show some descent approach with the crucial points commented:
function getAjaxObject(forLocalData) {
var isIE = /*@cc_on/*@if(@_jscript)true@else@*/false/*@end@*/;
var xhr = null;
if (typeof window == 'object') {
/* Common branch for modern browsers */
if (
(typeof window.XMLHttpRequest != 'undefined') &&
// window.XMLHttpRequest instances in IE do not
// have local file access even from local pages,
// unlike ActiveX instances: so if forLocalData
// flag set, we are trying to use the underlaying
// ActiveX constructor.
!(isIE && forLocalData)
) {
try {
return new window.XMLHttpRequest();
}
catch(e) {
return new Error(e.message);
}
}
/* ActiveX branch for old IE versions and for local data access
capable instances, see:
* http://blogs.msdn.com/xmlteam/archi...ht-version-of-msxml-in-internet-explorer.aspx
* http://groups.google.com/group/micr..._frm/thread/7772ac2ad016e2bf/5bd173be4950e107
* http://blogs.msdn.com/ie/archive/2006/01/23/516393.aspx
* for proper ProgID and proper ProgID trying sequence.
*/
else if (
(isIE) &&
(typeof window.ActiveXObject != 'undefined') // IE 5.x for MacOS
without ActiveX
) {
try {
return new window.ActiveXObject('Msxml2.XMLHTTP.6.0');
}
catch(e) {
try {
xhr = new window.ActiveXObject('Msxml2.XMLHTTP.3.0');
/* Msxml2.XMLHTTP.3.0 and older have XSL Patterns as the
* defailt XML language, not XPath, so fixing it:
*/
xhr.setProperty('SelectionLanguage', 'XPath');
return xhr;
}
catch(e) {
try {
xhr = new window.ActiveXObject('Msxml2.XMLHTTP');
/* Msxml2.XMLHTTP (IE6 / Win XP SP2 in default installation)
* have XSL Patterns as the defailt XML language, not XPath,
* but it doesn't support .setProperty switch.
* Setting the warning flag at the very least:
*/
arguments.callee.isXSLPatterns = true;
return xhr;
}
catch(e) {
/* Microsoft.XMLHTTP ProgID as the last ressort is not used
* as the security consideration should prevail over the
* maximum backward compatibility. Unlock for special cases only.
*/
//try {
// return new window.ActiveXObject('Microsoft.XMLHTTP');
//}
//catch(e) {
// return new Error(e.message);
//}
return new Error(e.message);
}
}
}
}
/* If nothing then nothing... */
else {
return new Error('ActiveX is missing or blocked');
}
}
else {
return new Error('window host object is missing');
}
}
Test (unzip and run AJAX/test/index.html):
http://sites.google.com/site/schoolsring/javascript
LocalDataReadingTest.zip
In each test:
1) attempt to read xml file in the same directory
2) attempt to read xml file in subdirectory
3) attempt to read xml file in top directory
4) attempt to read xml file in sibling directory
Each reading made twice: one time with without overriding MIME type,
next time with implied "text/xml" type.
1) IE
Internet Explorer 8.0 / Windows Vista SP2 - newest
Internet Explorer 6.0.2900 / Windows XP SP2 - legacy test
window.XMLHttpRequest in new IE does NOT allow local data reading at
all.
window.ActiveXObject(ProgID) doesn't support .overrideMimeType method
Without implied "text/xml" Content-type the results are:
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
OK attempt to read xml file in top directory
OK attempt to read xml file in sibling directory
success request status = 0
.requestXML is empty, a manual serialization from responseText is
needed later.
2) Fx
Mozilla Firefox 3.6.3 / Windows Vista SP2 - newest
Mozilla Firefox 3.5.7 / Windows XP SP2 - legacy test
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
_ attempt to read xml file in top directory (Error: Access to
restricted URI denied)
_ attempt to read xml file in sibling directory (Error: Access to
restricted URI denied)
success request status = 0
.requestXML is filled properly either with or without
using .overrideMimeType method
3) Sf
Apple Safari 4.0.5 / Windows Vista SP2
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
OK attempt to read xml file in top directory
OK attempt to read xml file in sibling directory
success request status = 0
.requestXML is filled properly either with or without
using .overrideMimeType method
4) Ch
Google Chrome 4.1.249 / Windows Vista SP2
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
OK attempt to read xml file in top directory
OK attempt to read xml file in sibling directory
success request status = 0
.requestXML is filled properly either with or without
using .overrideMimeType method
5) Op
Opera 10.51 / Windows Vista SP2
OK attempt to read xml file in the same directory
OK attempt to read xml file in subdirectory
OK attempt to read xml file in top directory
OK attempt to read xml file in sibling directory
success request status = 0
.requestXML is filled properly either with or without
using .overrideMimeType method
Conclusions:
1) IE sucks but usable
2) Fx is the most strict
3) overrideMimeType is useless: it is not needed where supported
and it is not supported where needed
4) The quality of XMLHttpRequest instantiation blocks in all
prominent libraries is *awful*... No, sorry: it is AWFUL. No... It is
a BLOODY AWFUL NIGHTMARE. The guys didn't read MSDN for years. If they
don't care about IE users whatsoever is fine, but they should
explicitly mark it then. I am posting the XHR init part from the test
file here - not as a sample of an outstanding coding, but at least to
show some descent approach with the crucial points commented:
function getAjaxObject(forLocalData) {
var isIE = /*@cc_on/*@if(@_jscript)true@else@*/false/*@end@*/;
var xhr = null;
if (typeof window == 'object') {
/* Common branch for modern browsers */
if (
(typeof window.XMLHttpRequest != 'undefined') &&
// window.XMLHttpRequest instances in IE do not
// have local file access even from local pages,
// unlike ActiveX instances: so if forLocalData
// flag set, we are trying to use the underlaying
// ActiveX constructor.
!(isIE && forLocalData)
) {
try {
return new window.XMLHttpRequest();
}
catch(e) {
return new Error(e.message);
}
}
/* ActiveX branch for old IE versions and for local data access
capable instances, see:
* http://blogs.msdn.com/xmlteam/archi...ht-version-of-msxml-in-internet-explorer.aspx
* http://groups.google.com/group/micr..._frm/thread/7772ac2ad016e2bf/5bd173be4950e107
* http://blogs.msdn.com/ie/archive/2006/01/23/516393.aspx
* for proper ProgID and proper ProgID trying sequence.
*/
else if (
(isIE) &&
(typeof window.ActiveXObject != 'undefined') // IE 5.x for MacOS
without ActiveX
) {
try {
return new window.ActiveXObject('Msxml2.XMLHTTP.6.0');
}
catch(e) {
try {
xhr = new window.ActiveXObject('Msxml2.XMLHTTP.3.0');
/* Msxml2.XMLHTTP.3.0 and older have XSL Patterns as the
* defailt XML language, not XPath, so fixing it:
*/
xhr.setProperty('SelectionLanguage', 'XPath');
return xhr;
}
catch(e) {
try {
xhr = new window.ActiveXObject('Msxml2.XMLHTTP');
/* Msxml2.XMLHTTP (IE6 / Win XP SP2 in default installation)
* have XSL Patterns as the defailt XML language, not XPath,
* but it doesn't support .setProperty switch.
* Setting the warning flag at the very least:
*/
arguments.callee.isXSLPatterns = true;
return xhr;
}
catch(e) {
/* Microsoft.XMLHTTP ProgID as the last ressort is not used
* as the security consideration should prevail over the
* maximum backward compatibility. Unlock for special cases only.
*/
//try {
// return new window.ActiveXObject('Microsoft.XMLHTTP');
//}
//catch(e) {
// return new Error(e.message);
//}
return new Error(e.message);
}
}
}
}
/* If nothing then nothing... */
else {
return new Error('ActiveX is missing or blocked');
}
}
else {
return new Error('window host object is missing');
}
}