Using XMLHttpRequest run locally for local data reading

N

nick

In relation to @end I tested it on IE6 / Win XP and your statement
about it being just closing part of @if-@elif-@else-@end block is
right. And @cc_on once used keeps pragma parser on till the end of
source. So either it was a change from IE5 to IE6 or - most probably -
bad documentation from the very beginning.

If my memories from the IE5/6 days serve me correctly, what you are
looking for is @cc_off.
 
V

VK

If my memories from the IE5/6 days serve me correctly, what you are
looking for is @cc_off.

YES!!! That was that and not @end
http://www.webdeveloper.com/forum/showthread.php?t=138110
http://www.frihost.com/forums/vt-60367.html

Now a million bucks question how did get into the picture in the
middle of this decade. IE6 and IE8 do not know it and report syntax
error. MSDN is dead silent about as well, at least now. Looks like yet
another urban legend. By tracing it down it could be possible to find
out how urban legends and cargo cults in coding being born. Most
definitely (and Google + WayBack Machine are my witnesses) I did not
participate in its creation. At least consciously...
 
V

VK

Uhmm... I guess I forgot to mention how to eventually get XML from
responseText for IE. IE's serialization ways are numerous, version-
dependent and boring. See for instance:
 http://groups.google.com/group/microsoft.public.xml/browse_frm/thread....

Because for other browsers this problem doesn't arise, we can be IE
specific and use quick, dirty and amusingly effective way using IE-
specific XML Data Islands tools and IE's <XML> tag.

So assuming we've got responseText filled, responseXML empty and we
know that it should be a well-formed XML file, we simply do that:

 var tmp = document.createElement('DIV');
 tmp.innerHTML = '<XML>' + xhr.responseText + '</XML>';
 return tmp.firstChild.XMLDocument.documentElement;

Respectively for a HTML fragment we can do:

 var tmp = document.createElement('DIV');
 tmp.innerHTML = xhr.responseText;
 return tmp.firstChild;

Enjoy! :)

I guess now it should be finally answered to the foka's OP that
started this:
http://groups.google.com/group/comp.lang.javascript/msg/08d156643e084fba
I'll post a copy to the relevant thread as well

1) OP path "../xml/groups.xml" is not universally possible because Fx
allows to read only in the same directory and subdirectories, but not
in top or sibling directories. So the directory structure has to be
arganized accordingly.

2) Let's have XML data in groups.xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<group>AK</group>
<name>COS AK</name>
</item>
<item>
<group>BZ</group>
<name>COS BZ</name>
</item>
<item>
<group>CA</group>
<name>COS CA</name>
</item>
</items>

3) Let's have a HTML page with the script in the same directory where
groups.xml is. Then to read XML data and to populate select form
element on the page the script could be like this (tested IE6, IE8,
Fx, Sf, Ch, Op):

<!DOCTYPE html>
<html>
<head>
<title>XMLHttpRequest local XML file reading test</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function getAjaxObject(forLocalData) {

var f = arguments.callee;

var isIE = /*@cc_on true || @*/ false;

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:
* 1) http://tinyurl.com/MSDNBlog1
* 2) http://tinyurl.com/MSNewsGroup1
* 3) http://tinyurl.com/MSDNBlog2
* for proper ProgID and proper ProgID trying sequence.
*
* Correction from the test lab: contrary to what MS says
* and in support of my arguments at 2) discission a set
* of machines with IE6 / Win XP SP2 in default installation
* failed to puck up on Msxml2.XMLHTTP.3.0 so used the older
* version: yet they happily picked up on Msxml2.XMLHTTP.4.0
* Whatever the exact reasons of it are, a branch for 4.0
* is added to ensure the newest version usage in all
* given circumstances.
*/
else if (
(isIE) &&
/* if IE 5.x for MacOS without ActiveX: */
(typeof window.ActiveXObject != 'undefined')
) {
try {
xhr = new window.ActiveXObject('Msxml2.XMLHTTP.6.0');
f.isActiveX = true;
f.ActiveXVersion = 6;
return xhr;
}
catch(e) {
try {
xhr = new window.ActiveXObject('Msxml2.XMLHTTP.3.0');
/* Contrary to numerous MS claims like here:
* http://tinyurl.com/SelectionLanguage
* Msxml2.XMLHTTP.3.0 (IE6 / Win XP SP2 by default)
* does not have XSL Patterns as the default language
* neither it supports setProperty method so trying
* to apply setProperty("SelectionLanguage", "XPath")
* leads to "not supported" runtime error on IE6.
* In the reality the last COM with XSL Patterns were
* in XMLHTTP.2.x versions. Looks pretty much as a MS
* internal urban legend.
*/
f.isActiveX = true;
f.ActiveXVersion = 3;
return xhr;
}
catch(e) {
/* As per MS Security Bulletin http://tinyurl.com/MS06-061
* Msxml2.XMLHTTP.2.x and older like Microsoft.XMLHTTP can
* be blocked and overall the security considerations
* should prevail over the maximum backward compatibility.
* Add these branches only if you really need to support
* IE 5.x in a non-commercial home use solution.
*/
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');
}
}


window.onload = function() {
document.forms[0].reset();
window.setTimeout('test()', 10);
}


function test() {

var isIE = /*@cc_on true || @*/ false;

var wantXML = true;

var xmlDoc = null; var tmp = null;

if ((xhr = getAjaxObject(true)) instanceof Error) {
window.alert('Couldn\'t instantiate XHR:\n' + xhr.message);
return;
}

if (loadData('groups.xml') instanceof Error) {
window.alert('Couldn\'t read data:\n' + xhr.message);
return;
}

if (isIE && wantXML) {
tmp = document.createElement('DIV');
tmp.innerHTML = '<XML>'+xhr.responseText+'</XML>';
xmlDoc = tmp.firstChild.XMLDocument.documentElement;
}
else {
xmlDoc = xhr.responseXML;
}

var out = document.forms[0].groups;
var groupXml = xmlDoc.getElementsByTagName('group');
for (var i=0; i<groupXml.length; i++) {
out.options = new Option(groupXml.firstChild.nodeValue,
groupXml.firstChild.nodeValue);
}

function loadData(path, wantXML) {
try {
xhr.open('GET', path, false);
xhr.send('');
}
catch(e) {
return new Error(e.message);
}
}
}
</script>
</head>

<body>
<h1>XMLHttpRequest local XML file reading test</h1>
<form action="" onsubmit="return false;">
<fieldset>
<select name="groups">
</select>
</fieldset>
</form>
</body>
</html>
 
V

VK

*
 * Correction from the test lab: contrary to what MS says
 * and in support of my arguments at 2) discission a set
 * of machines with IE6 / Win XP SP2 in default installation
 * failed to puck up on Msxml2.XMLHTTP.3.0 so used the older
 * version: yet they happily picked up on Msxml2.XMLHTTP.4.0
 * Whatever the exact reasons of it are, a branch for 4.0
 * is added to ensure the newest version usage in all
 * given circumstances.

Please disregard this comment: that was a false alarm from an altered
configuration. I thought I deleted it but obviously I didn't. Sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top