Re: Netscape compatibility
On Sat, 9 Oct 2004 09:33:05 -0400, Roger Desparois
<roger.desparoi s@sympatico.ca> wrote:
[snip]
[color=blue]
> NS won't recognize it.
>
> Can anyone tell me why ?[/color]
The document.all collection is a Microsoft invention, support by only a
few other browsers.
[color=blue]
> And is there a way around the problem ?[/color]
Use the W3C Document Object Model (DOM). Whilst support is not 100%, it is
better than the all collection. Optionally, you can add fallbacks to other
element retrieval methods.
<URL:http://www.jibbering.c om/faq/faq_notes/not_browser_det ect.html#bdGEID >
[color=blue]
> I really like the elegance of this code and would like to keep using it.[/color]
I'm afraid that a lack of feature testing and unnecessary use of eval is
not elegant.
[color=blue]
> <head
> <script language="Javas cript"`>[/color]
The language attribute is deprecated and shouldn't be used any more. Use
the (required) type attribute instead.
<script type="text/javascript">
[color=blue]
> <!--[/color]
Script hiding is another obsolete practice, unnecessary for the Web today.
Remove the SGML comments.
[color=blue]
> function Pop(name){
> eval("document. all."+name+".st yle.visibility= 'visible'");[/color]
The eval here, and below, isn't necessary. Properties can be accessed
using the bracket notation which allows the use of expressions to
construct the property name (see
<URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>).
document.all[name].style.visibili ty = 'visible';
However, this still lacks feature detection and a cross-browser approach
to accessing elements.
Before your code, you can add this:
var getRefById = document.getEle mentById ?
function(n) {return document.getEle mentById(n);}
: document.all ?
function(n) {
var e = document.all[n] || {}, l = e.length;
if('number' == typeof l) {
for(var i = 0; i < l; ++i) {if(e[i].id == n) {return e[i];}}
} else if(e.id == n) {return e;}
return null;
}
: function() {return null;},
It creates a function, getRefById, that will attempt to obtain a function
reference when given the id of an element. Once you have that reference,
you check that the style object is available, and if so, use it.
function Pop(name) {
/* Try to get the element identified as name. */
var e = getRefById(name ), s;
/* If a reference was obtained... */
if(e) {
/* ...check for the style property. */
if(e.style) {
s = e.style;
} else {
/* If it doesn't exist, use the element itself. */
s = e;
}
s.visibility = 'visible';
}
}
This can be reduced to:
function Pop(name) {
var e = getRefById(name );
if(e) {(e.style || e).visibility = 'visible';}
}
The main difference is the use of the logical OR operator, '||'. Rather
than operating on booleans only, it evaluates the 'trueness' of the
operands. If the first operand evaluates to true, its value (the style
object in this case) is the result. If the first operand evaluates to
false, the second value is always returned.
[color=blue]
> }
> function Wop(name){
> eval("document. all."+name+".st yle.visibility= 'hidden'");
> }[/color]
The above obviously applies here, too.
[snip]
[color=blue]
> <div id="prod"
> Style="backgrou nd-color:#ff0000;P osition:Absolut e;Left:39px;
> Top:89px;Width: 178px;Height:16 px;Visibility:v isible;z-index:31"[/color]
Why this massive amount inline style information? Comparing it to the DIV
below, only the z-index and the visibility property seem to be different.
Move it into a style sheet and use a class selector. The remaining unique
information can be applied with an id selector.
[snip]
[color=blue]
> <table width="178" height="16" border="0" cellspacing="0"
> cellpadding="0" class="menu">
> <tr>
> <td align="center" valign="middle" >Volleyball</td>[/color]
Why use deprecated attributes on the table and cell elements when you're
already using CSS?
[snip]
Hope that helps,
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
On Sat, 9 Oct 2004 09:33:05 -0400, Roger Desparois
<roger.desparoi s@sympatico.ca> wrote:
[snip]
[color=blue]
> NS won't recognize it.
>
> Can anyone tell me why ?[/color]
The document.all collection is a Microsoft invention, support by only a
few other browsers.
[color=blue]
> And is there a way around the problem ?[/color]
Use the W3C Document Object Model (DOM). Whilst support is not 100%, it is
better than the all collection. Optionally, you can add fallbacks to other
element retrieval methods.
<URL:http://www.jibbering.c om/faq/faq_notes/not_browser_det ect.html#bdGEID >
[color=blue]
> I really like the elegance of this code and would like to keep using it.[/color]
I'm afraid that a lack of feature testing and unnecessary use of eval is
not elegant.
[color=blue]
> <head
> <script language="Javas cript"`>[/color]
The language attribute is deprecated and shouldn't be used any more. Use
the (required) type attribute instead.
<script type="text/javascript">
[color=blue]
> <!--[/color]
Script hiding is another obsolete practice, unnecessary for the Web today.
Remove the SGML comments.
[color=blue]
> function Pop(name){
> eval("document. all."+name+".st yle.visibility= 'visible'");[/color]
The eval here, and below, isn't necessary. Properties can be accessed
using the bracket notation which allows the use of expressions to
construct the property name (see
<URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>).
document.all[name].style.visibili ty = 'visible';
However, this still lacks feature detection and a cross-browser approach
to accessing elements.
Before your code, you can add this:
var getRefById = document.getEle mentById ?
function(n) {return document.getEle mentById(n);}
: document.all ?
function(n) {
var e = document.all[n] || {}, l = e.length;
if('number' == typeof l) {
for(var i = 0; i < l; ++i) {if(e[i].id == n) {return e[i];}}
} else if(e.id == n) {return e;}
return null;
}
: function() {return null;},
It creates a function, getRefById, that will attempt to obtain a function
reference when given the id of an element. Once you have that reference,
you check that the style object is available, and if so, use it.
function Pop(name) {
/* Try to get the element identified as name. */
var e = getRefById(name ), s;
/* If a reference was obtained... */
if(e) {
/* ...check for the style property. */
if(e.style) {
s = e.style;
} else {
/* If it doesn't exist, use the element itself. */
s = e;
}
s.visibility = 'visible';
}
}
This can be reduced to:
function Pop(name) {
var e = getRefById(name );
if(e) {(e.style || e).visibility = 'visible';}
}
The main difference is the use of the logical OR operator, '||'. Rather
than operating on booleans only, it evaluates the 'trueness' of the
operands. If the first operand evaluates to true, its value (the style
object in this case) is the result. If the first operand evaluates to
false, the second value is always returned.
[color=blue]
> }
> function Wop(name){
> eval("document. all."+name+".st yle.visibility= 'hidden'");
> }[/color]
The above obviously applies here, too.
[snip]
[color=blue]
> <div id="prod"
> Style="backgrou nd-color:#ff0000;P osition:Absolut e;Left:39px;
> Top:89px;Width: 178px;Height:16 px;Visibility:v isible;z-index:31"[/color]
Why this massive amount inline style information? Comparing it to the DIV
below, only the z-index and the visibility property seem to be different.
Move it into a style sheet and use a class selector. The remaining unique
information can be applied with an id selector.
[snip]
[color=blue]
> <table width="178" height="16" border="0" cellspacing="0"
> cellpadding="0" class="menu">
> <tr>
> <td align="center" valign="middle" >Volleyball</td>[/color]
Why use deprecated attributes on the table and cell elements when you're
already using CSS?
[snip]
Hope that helps,
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Comment