Dante wrote:
> I was trying to improve PPK's JIR script by using XML.
#define PPK
#define JIR
> I have this code:
> function init()
> {
> var x = document.getElementsByTagName('replace');
^
> var prefix = 'http://www.quirksmode.org/dom/pix/';
> var suffix = '.gif';
> for (var i=0;i<x.length;i++)
^
It is wise to check references before accessing the objects/properties
they refer to:
if (typeof document.getElementsByTagName != "function"
|| typeof document.getElementsByTagName != "object")
{
var x = document.getElementsByTagName('replace');
if (x)
{
...
}
}
While the first test may not be required (depending on the DOM of the
target UA), the second one is duty.
> {
> if (x[i].getAttribute('url'))
Do not use getAttribute(...) if you can avoid it, the so-called most
used browser (IE)'s implementation of the W3C-DOM is flawed on it
Use
if (x[i].url)
instead.
Also note that it is unwise not to give variables a talking name. To
know what `x' refers to, one must re-read the preceding statements.
Time is money.
> {
> var y = replace.cloneNode(true);
> y.replacedHeader = x[i];
Again, you should check if the cloneNode(...) process succeeded:
if (y)
> outimage = prefix + x[i].getAttribute('url') + suffix;
Again, drop getAttribute(...) if you can. Besides, is it intended
that `outimage'
> outalt = x[i].firstChild.nodeValue;
and `outalt' are declared global? If not, use the `var' keyword.
> var replace='<img src="'+outimage+'" alt="'+outalt+'">';
> y.onload = function () {
> this.replacedHeader.innerHTML=replace;
`innerHTML' is a proprietary property only available in the HTML DOM of
some UAs. Use standardized W3C-DOM properties and methods instead:
// Create the `img' element
var oImg = document.createElement("img");
if (oImg) // if that worked
{
// assign values to its properties
oImg.src = outimage;
oImg.alt = outalt;
// remove all child nodes from the later parent element
var o = this.replacedHeader;
while (o.childNodes.length > 0)
o.removeChild(o.firstChild);
// and append the `img' element as its child node
o.appendChild(oImg);
}
> I'm sure there's plenty of things wrong with it that I missed...is there?
Yes, there is, for suitable values of `plenty'
Have a look in the specification before you continue:
http://www.w3.org/TR/DOM-Level-2-Core/
PointedEars