How To Use |
Include the necessary JavaScript files in the header of page (put all code in an only one file if you want)
You can decide not to include all files if the page is dynamically generated (with PHP,ASP,JSP...)
Files needed for each browser:
Files\Browser | MSIE 4,5,6 | NS 6.x/Mozilla |
_ie4_ | X | |
_ns6_ | X | |
_dom_ | X | X |
_unbound_ | X |
xpdom_init.js is needed by all browsers
Current file organization is due to convenience of the development, but you might put all code of _ie4_ files in an only _ie4_ file, same to _dom_ and so on
Define a method that is called by onload event of BODY tag and call xpdom_init()
Example:
<html> <head> ... <script language="JavaScript"> var xpdom_ptr = null; function uninit() { xpdom_finalize(xpdom_ptr); // Only necessary with MSIE 4 and up } function init() { xpdom_ptr = xpdom_init(window); } </script> </head> <body onload="init();" onunload="unint();"> ...
If you want to use position properties of elements on loading time, there is a MSIE 4 bug: position properties are not defined until onload event ends.
A work around to solve this (and compatible with all browsers) is delaying with a timer event :
function init() { xpdom_ptr = xpdom_init(window); setTimeout("init_delayed()",50); } function init_delayed() { // Document is completely loaded, MSIE 4 properties are valid ... /* Access to elements */ }
Sharing the library with frames:
XPDOM is ready to use with frames sharing the included source in the top frameset, and used in frames
Example:
var xpdom_ptr = null; function uninit() { window.parent.xpdom_finalize(xpdom_ptr); xpdom_ptr = null; } function init() { xpdom_ptr = window.parent.xpdom_init(window); }
Access to DOM elements
XPDOM works with wrappers, each native element has a XP-DOM wrapper
A wrapper can be used as it was a Java object using Java W3C-DOM binding but with JavaScript (no types...)
To get the wrapper of a native element:
var aWrappedElement = aNativeElem.getDOMWrapper();
To get the native element of a wrapper:
var aNativeElement = aWrappedElement.getNative();
If a method is not implemented with XPDOM but it exists in native, use getNative():
var target = aWrappedFormElement.getNative().target;
Rule: a wrapped element (a XPDOM object) returns only wrapped elements with its methods
Rule: only native elements used have a wrapped object associated (by memory and performance reasons)
To start access objects inside the XPDOM environment get the document and window associated wrapper with:
var doc = document.getDOMWrapper(); var view = window.getDOMWrapper(); view = doc.getDefaultView(); // Alternative and standard way to do the same
And get elements by example:
var aWrappedElement = doc.getElementById("myelement");
To see more complicated examples and real use of XPDOM see the example widgets developed on top of XP-DOM