
Originally Posted by
OBCENEIKON
Here is that specific block of code
I really dislike this function, so I'll provide an alternative later. At a quick glance, there is nothing wrong as such with this code, though the unnecessary use of eval will cause problems in some situations. I'll cover that when I get there.
The current problem with the code at your site is that you call layerObject without providing an expected argument, id. You don't seem to be using showhide anywhere.
I'm curious to know why that document is so heavily scripted. You apparently have the ability to process information on the server, so why rely so much on the client? The client is unreliable. The server isn't.
Using a global for a function that could be called for multiple elements is a really stupid idea. If your hidden elements default to 'none' in a style sheet rule, you can easily determine that from the style object. The following example omits feature detection for clarity.
Code:
var state;
if('block' == elementRef.style.display) {
state = 'none';
} else { /* 'none' or an empty string */
state = 'block';
}
Or, more compactly:
Code:
var state = ('block' == elementRef.style.display)
? 'none' : 'block';
if (state == 'block') {
state = 'none';
} else {
state = 'block';
}
As evidenced above, I think it better to write that using the conditional operator (<expr> ? <expr> : <expr>).
if (document.all) { //IS IE 4 or 5 (or 6 beta)
Incorrect. That would also evaluate to include Opera and IceBrowser, amongst others. Use this sort of thing to determine what a user agent supports, not what it is. The former matters. The latter doesn't.
eval("document.all."+ layer_ref +".style.display = state");
The problem with this eval statement is what if layer_ref contains 'invalid' characters, such as dots (.), hyphens (-), and so on? The host will then evaluate code such as
Code:
document.all.id-5.style.display = state;
which would be interpreted as, "assign state to 5.style.display and subtract that from document.all.id", which is complete nonsense.
The far superior alternative is:
Code:
document.all[layer_ref].style.display = state;
which will never suffer the previous problem.
These should really be a series of exclusive code branches. If a user agent took one of the previous routes, why should it take another one?
So, the replacement:
Code:
function toggleDisplay(element) {var style;
if('string' == typeof element) {
if(document.getElementById) {
element = document.getElementById(element);
} /* else if(document.all) {
element = document.all[element];
} else if(document.layers) {
element = document.layers[element];
} */
}
if(element) {
style = element.style || element;
style.display = ('block' == style.display)
? 'none' : 'block';
}
}
I've effectively removed the document.all and document.layers branches as they shouldn't really be necessary any more. If you want to keep them, remove the comment delimiters.
I could take this function further, but it will probably cater for your needs.
If none of this helps, please post a URL to a simple demonstration that shows the problem clearly.
Mike
Bookmarks