Discussion on:

7
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
Early versions of Gecko did not support async but it has been supported for some time. (Since v1.4 from memory.) This means you can use code like:

function loadXML(xmlFile) {
// loads XML document - RSS
if ((typeof document.implementation == "object") && (typeof document.implementation.createDocument == "function"))
// Mozilla based browser
xmlDoc = document.implementation.createDocument("", "", null);
else
// assume IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}

Now if you want to be clever, how can I do this in Opera? It doesn't support createDocument() or XML.
Hi irongut,

This option will work only on Windows so it?s not an option for a lot of people.
The Gecko choice is to be multiplataform!
Health,
Josir
Hi josir,

If you read my code carefully, I'm not using an ActiveX object in Gecko but I am in IE. Here's the code again:

function loadXML(xmlFile) {
// loads XML document
if ((typeof document.implementation == "object") && (typeof document.implementation.createDocument == "function"))
// Mozilla based browser
xmlDoc = document.implementation.createDocument("", "", null);
else
// assume IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}

First it checks to see if the browser supports document.implementation.createDocument.

If it does then it is probably a Gecko based browser so it uses document.implementation.createDocument to create a document.

If not then I assume IE and use new ActiveXObject("Microsoft.XMLDOM") to create a document.

Then either browser allows me to set synchronous loading and to load the document using xmlDoc.load(xmlFile).

This is an effective way to load XML into either IE or Gecko based browsers (on any platform). It has been running on my sites for a year now without any problems. If only Opera would support these methods then it would work for their browser to.

It would look neater (and be easier to read) if Builder would allow me to indent the code properly.
0 Votes
+ -
Opera v8.0 supports document.implementation.createDocument but I still can't get my above code to work. It fails on xmlDoc.load(xmlFile) claiming a type mismatch but that line works fine in IE, Mozilla and Firefox.

Anyone got any ideas?
0 Votes
+ -
First of all: very nice article.

But I have a question: where the xml file is located : on server or on the client computer ?

That is: Is it possible to use a URL on the load method ?
0 Votes
+ -
The XML file is located on the server. Browser-based JavaScript doesn't allow programmed access to the client's file system.
0 Votes
+ -
XML file location
irongut 5th Dec 2004
The XML file must also be on the same host as the javascript otherwise you'll fall foul of the security sandbox. (on Gecko based browsers at least)
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.