importXML works in-line, but not externally

K

Kevin

Hi,

Meet importXML (see below). It merely imports an XML file to the
object, "xmlDoc."

When I call this function in-line, i.e. when I include the whole
importXML.js in the <HEAD>, I can access the XML elements sucessfully.
However, when I call this function externally, i.e. I include it in
the <HEAD> as a <javascript src = ".../importXML.js>, it fails to
work. It fails because alerts tell me that a sample XML file has a
length of 19, when in fact it has a length of 9. I confidently say
that it's 9 because the function worked correctly, plus I'm looking at
the XML file now, and it has length 9!

Please tell me why you think the length is showing up as 19 rather
than 9, i.e. why does the function work in-line but not externally?
Note that when I say externally, I mean that I include it in the
<HEAD>, then call the function in a <javascript ...> tag within
<HEAD>.

function importXML(file) {
var xmlDoc;
var moz = (typeof document.implementation != 'undefined') &&
(typeof
document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
if (moz) {
xhttp=new XMLHttpRequest();
xhttp.open("GET",file,false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

} else if (ie) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};
xmlDoc.load(file);
}
return xmlDoc;
}

function importXML(file) {
var xmlDoc;
var moz = (typeof document.implementation != 'undefined') &&
(typeof
document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
if (moz) {
xhttp=new XMLHttpRequest();
xhttp.open("GET",file,false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

} else if (ie) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};
xmlDoc.load(file);
}
return xmlDoc;
}

Thank you,
Kevin
 
D

David Mark

Kevin said:
Hi,

Meet importXML (see below). It merely imports an XML file to the
object, "xmlDoc."

When I call this function in-line, i.e. when I include the whole
importXML.js in the <HEAD>, I can access the XML elements sucessfully.
However, when I call this function externally, i.e. I include it in
the <HEAD> as a <javascript src = ".../importXML.js>, it fails to
work. It fails because alerts tell me that a sample XML file has a
length of 19, when in fact it has a length of 9. I confidently say
that it's 9 because the function worked correctly, plus I'm looking at
the XML file now, and it has length 9!

What alerts?
Please tell me why you think the length is showing up as 19 rather
than 9, i.e. why does the function work in-line but not externally?
Note that when I say externally, I mean that I include it in the
<HEAD>, then call the function in a <javascript ...> tag within
<HEAD>.

function importXML(file) {
var xmlDoc;
var moz = (typeof document.implementation != 'undefined') &&
(typeof
document.implementation.createDocument != 'undefined');

That doesn't indicate Mozilla.
var ie = (typeof window.ActiveXObject != 'undefined');

That doesn't indicate IE.

http://www.jibbering.com/faq/notes/detect-browser/#bdOI
if (moz) {
xhttp=new XMLHttpRequest();

Bad inference.
xhttp.open("GET",file,false);

Don't use synchronous XHR. It blocks the browser UI, which is out of
bounds for a script.
xhttp.send("");

Bad send (use null);
xmlDoc=xhttp.responseXML;

} else if (ie) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

Bad inference.
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};

Why are you polling the readyState if it isn't an asynchronous load?
xmlDoc.load(file);
}
return xmlDoc;
}

function importXML(file) {

Haven't we met before? :)

[...]

Post the version that illustrates the issue (i.e. the one with the alerts).
 

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
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top