How do I make this work for W3C?

D

David W. Deley

What do I put for the W3C part of this code?

function dot(i)
{
this.X = Xpos;
this.Y = Ypos;
this.dx = 0;
this.dy = 0;
if (isNetscape4) {
this.obj = eval("document.dot" + i);
} else if (isIE) {
this.obj = eval("dot" + i + ".style");
} else if (isW3C) {
this.obj = ?????????????
}
}

var dots = new Array(nDots);
var i = 0;
for (i = 0; i < nDots; i++) {
dots = new dot(i);
}
 
L

Lasse Reichstein Nielsen

David W. Deley said:
What do I put for the W3C part of this code?

Code is *not* obvious, especially without comments. That means
we have to guess what you want the code to do.
function dot(i)
{
this.X = Xpos;
this.Y = Ypos;
this.dx = 0;
this.dy = 0;
if (isNetscape4) {

Any browser detection almost almways have failures. Trying to detect
Netscape 4 is not as efficient as detecting the absence of better
approaches and testing for the success of this one.
this.obj = eval("document.dot" + i);

Never use eval to access properties. Never! (With overwhelming
probability, you'll never need to use eval at all, since there are
safer and more efficient alternatives to all the common (mis)uses of
eval).
} else if (isIE) {
this.obj = eval("dot" + i + ".style");
} else if (isW3C) {
this.obj = ?????????????

I assume that you are trying to find an element with, e.g., id="dot2".

I recommned this approach:
---
var id = "dot"+i;
if (document.getElementById) {
this.obj = document.getElementById(id);
} else if (document.all) {
this.obj = document.all[id];
} else if (document[id]) {
this.obj = document[id];
}
---
No browser detection. Testing for known access methods first and
falling back on less standardized methods. If a browser has both
document.getElementById and document.layers, it will use the standard
method (yes, such browsers exist, and would be likely to be
misidentified as Neetscape 4).
dots = new dot(i);


Traditionally, functions used as constructors are capitalized, so
I would change "dot" to "Dot".

/L
 
D

David W. Deley

Fabulous. That should work great. Thank you. (I'm modifying code
someone else wrote.) -D.D.
David W. Deley said:
What do I put for the W3C part of this code?

Code is *not* obvious, especially without comments. That means
we have to guess what you want the code to do.
function dot(i)
{
this.X = Xpos;
this.Y = Ypos;
this.dx = 0;
this.dy = 0;
if (isNetscape4) {

Any browser detection almost almways have failures. Trying to detect
Netscape 4 is not as efficient as detecting the absence of better
approaches and testing for the success of this one.
this.obj = eval("document.dot" + i);

Never use eval to access properties. Never! (With overwhelming
probability, you'll never need to use eval at all, since there are
safer and more efficient alternatives to all the common (mis)uses of
eval).
} else if (isIE) {
this.obj = eval("dot" + i + ".style");
} else if (isW3C) {
this.obj = ?????????????

I assume that you are trying to find an element with, e.g., id="dot2".

I recommned this approach:
---
var id = "dot"+i;
if (document.getElementById) {
this.obj = document.getElementById(id);
} else if (document.all) {
this.obj = document.all[id];
} else if (document[id]) {
this.obj = document[id];
}
---
No browser detection. Testing for known access methods first and
falling back on less standardized methods. If a browser has both
document.getElementById and document.layers, it will use the standard
method (yes, such browsers exist, and would be likely to be
misidentified as Neetscape 4).
dots = new dot(i);


Traditionally, functions used as constructors are capitalized, so
I would change "dot" to "Dot".

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top