document.load()
Peter Kelly
pmk at kde.org
Sat Jun 21 11:03:30 BST 2003
Due to popular demand, document.load() has now been implemented.
Here's some brief notes on how to use it. See khtmltests/xml/loadxml.html
for a working example.
There are two ways of using load: synchronously and asynchronously. In
asynchronous mode, the call to load() starts the process but does not wait
for the document to finish. You need to register an event listener that
gets activated when the document loads.
xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.addEventListener("load",docLoaded,false);
xmlDoc.load(filename);
function docLoaded(event)
{
// ...
}
Errors are reported in a similar manner, through the normal event
dispatching process.
xmlDoc.addEventListener("load",docError,false);
function docError(event)
{
alert("Could not load document: " + event);
// ...
}
When used in synchronous mode, the load() method does not return until the
document has completed. You can therefore access the document straight
afterwards in your script.
xmlDoc.async = false; // async defaults to true
xmlDoc.load(filename);
alert(xmlDoc.documentElement.nodeName);
Events are still dispatched in synchronous mode. You may wish to still
trap error events as load() will return when the operation is complete,
whether or not the document was loaded successfully. In mozilla an
exception is thrown, however according to the spec this is incorrect,
so KHTML instead dispatches the error event.
Note that for security reasons it is only possible to load a document from
the same server. Also, load() only works with XML documents - HTML
documents are not supported.
For more detailed info see
http://www.w3.org/TR/DOM-Level-3-LS/load-save.html
--
Peter Kelly
pmk at kde.org
More information about the kfm-devel
mailing list