473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

eXtensible Image Replacement

I was trying to improve PPK's JIR script by using XML. 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++)
{
if (x[i].getAttribute('url'))
{
var y = replace.cloneNode(true);
y.replacedHeader = x[i];
outimage = prefix + x[i].getAttribute('url') + suffix;
outalt = x[i].firstChild.nodeValue;
var replace='<img src="'+outimage+'" alt="'+outalt+'">';
y.onload = function () {
this.replacedHeader.innerHTML=replace;
};
}
}
}
I'm sure there's plenty of things wrong with it that I missed...is there?
Jul 20 '05 #1
3 1378
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
Jul 20 '05 #2
I forgot to mention:

The original plan was to use document.createElement. But due to
name-spacing difficulties I modified it. Would
document.createElement('html:img') work if the html namespace was
defined?
Jul 20 '05 #3
pi******@reno.com (Dante) writes:
The original plan was to use document.createElement. But due to
name-spacing difficulties I modified it. Would
document.createElement('html:img') work if the html namespace was
defined?


I have no idea. It doesn't say it should in the W3C DOM Core
specification.
However, it has
document.createElementNS(htmlNamespaceURI,"img")
If you application is HTMLonly, you don't need it, but otherwise
you should use the "NS" methods.

W3C DOM 2 Core: <URL:http://www.w3.org/TR/DOM-Level-2-Core/core.html>

?L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Kerberos | last post by:
I followed Dan Cederholm's image replacement tutorial, to replace a header tag by a logo. The h1 is clickable if no CSS is applied but it I replace it by the logo, the area isn't clickable anymore...
12
by: Charlie King | last post by:
As I understand it, the use of FIR* to replace heading tags with images in visually enabled CSS browsers is now frowned upon on the basis that some screen readers will *nor* read back text that is...
5
by: Martin Eyles | last post by:
I have made a website that uses image replacement for the two main headings. It works beautifully in firefox, but in internet explorer the images appear too far to the right. Does anyone have any...
16
by: Alan Silver | last post by:
Hello, At one time, image replacement was recommended as being a good way to have well structured markup, whilst giving the user something pleasant visually. However, I have seen a few articles...
15
by: Alan Silver | last post by:
Hello, I am trying to do a simple (ha ha) bit of image replacement, so that the 'v' in a string of text inside an <h3> is replaced with an image of a tick that looks like a 'v' to fit with a...
1
by: rajbala.3399 | last post by:
Hai all, I need to get multiple image buttonsand wheni click on the image button it should display corresponding image on window and if i click another image button it is also display on same...
6
by: Rob | last post by:
Hello, I'm sure this has come up before. I have need for a collection of all elements/objects in an HTML document that have any kind of an attribute (HTML or CSS) that is making use of a URL to...
1
by: rsteph | last post by:
I've got some product information pages, with images and text, all setup within a table. I'm trying to add a small image in the upper right hand corner of the content div (where all the important...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.