A problem with an apparently common JS menu...

B

Bryan

I am attempting to make a web page more Netscape friendly... <a
href="http:www.gordonceilings.com"></a>

I have already corrected the table issues in an offline staging site
(where I do the building and then move it to the live site when ready),
so don't worry about those. My problem lies in the menu on the left. It
shows up and the initial links work in Netscape 8, but the menu will
not drop down to show the sub-selections. I can get the sub-menus to
drop down with "document.getElementById" arguments, but the rest of the
menu won't move down accordingly to make room, making for a jumbled
mess. It works flawlessly in IE5 and 6 with either document.all or
getElementById, but in looking for an answer I see that IE is more
forgiving...

Here is the script, it seems to be a common one:

<SCRIPT LANGUAGE="JavaScript">



// ADDITIONAL NOTES
// The input variables to the toggle function are the number of the
submenu to open/close,
// starting with 0, and the number of pixels to move the objects below.
// For example toggle(1,60) opens/closes the second submenu and moves
the objects below 60 pixels.

var nom = 10; // Number of menus
var usePictures = 0; // use pictures? 1 = yes, 0 = no

var ttls = new Array(); // An array for the title objects
var subs = new Array(); // An array for the submenu objects
var lastn;
var lastmove;

if (document.layers) {
visible = 'show';
hidden = 'hide';
}
else
if (document.all) {
visible = 'visible';
hidden = 'hidden';
}
for (var i = 1; i <= nom; i++) {
ttls = ('title' + i);
subs = ('submenu' +i);
}
function picopen(n) {
title = ('title' + n);
pic = ('pic' + n);
if (document.layers) {
document.layers[title].document.images[pic].src = "opened.gif";
}
else if (document.all) {
document.all(pic).src = "opened.gif";
}
}
function picclose(n) {
title = ('title' + n);
pic = ('pic' + n);
if (document.layers) {
document.layers[title].document.images[pic].src = "closed.gif";
}
else if (document.all) {
document.all(pic).src = "closed.gif";
}
}
lastn = (nom + 1);
lastmove = 0;
function lasttoggle(n,move) {
if (n <= nom) {
menu = ('submenu' + n);
if (document.layers) {
submenu = document.layers[menu];
}
else if (document.all) {
submenu = document.all(menu).style;
}
if (submenu.visibility == visible) {
submenu.visibility = hidden;
for (var i = (n+1); i <= nom; i++) {
if (document.layers) {
document.layers[ttls].top -= move;
document.layers[subs].top -= move;
}
else if (document.all) {
document.all(ttls).style.pixelTop -= move;
document.all(subs).style.pixelTop -= move;
}
}
}
}
}
function toggle(n,move) {
menu = ('submenu' + n);
if (document.layers) {
submenu = document.layers[menu];
}
else if (document.all) {
submenu = document.all(menu).style;
}
if (submenu.visibility == visible) {
submenu.visibility = hidden;
if (usePictures) picclose(n);
for (var i = (n+1); i <= nom; i++) {
if (document.layers) {
document.layers[ttls].top -= move;
document.layers[subs].top -= move;
}
else if (document.all) {
document.all(ttls).style.pixelTop -= move;
document.all(subs).style.pixelTop -= move;
}
}
}
else {
submenu.visibility = visible;
if (usePictures) picopen(n);
if (lastn != n) {
lasttoggle(lastn,lastmove);
}
for (var i = (n+1); i <= nom; i++) {
if (document.layers) {
document.layers[ttls].top += move;
document.layers[subs].top += move;
}
if (document.all) {
document.all(ttls).style.pixelTop += move;
document.all(subs).style.pixelTop += move;
}
}
}
lastn = n;
lastmove = move;
}


function loadFrames(frame1,page1) {
eval("parent."+frame1+".location='"+page1+"'");
}


// End -->





</script>

So far all I can find concerning this script is people that are having
problems with it... maybe it's time for a new script, but I really
don't want to have to rebuild the menu...
 
B

Bryan

Oh yeah, you need to click on the Gordon Interior Specialties logo on
the entrance page... the one with the red G...
 
D

David Dorward

Bryan said:
if (document.layers) {
if (document.all) {
maybe it's time for a new script

Lots of document.layers (NS4.x only) and lots of document.all (IE4 only[1]),
but not a lot of W3C DOM. It was time for a new script several years ago.

[1] Other browsers do support it, although I seem to recall that only IE
returns true if you test for it, other browsers assume that if you are
smart enough to test for support, then you are smart enough to use the W3C
DOM.
 
B

Bryan

Like I said, I can get the menus to appear in Netscape using
"document.getElementById" instead of "document.all", but the rest of
the menu won't drop down to accommodate it. Everything I'm seeing tells
me to use "document.getElementById", and I do, but nothing tells me how
to make the menu move for the visible layer.

Are the "document.layers" entries necessary? Maybe they're
interfering...
 
B

Bryan

OK, I have set up the menu like so:

<SCRIPT LANGUAGE="JavaScript">



// ADDITIONAL NOTES
// The input variables to the toggle function are the number of the
submenu to open/close,
// starting with 0, and the number of pixels to move the objects below.
// For example toggle(1,60) opens/closes the second submenu and moves
the objects below 60 pixels.

var nom = 10; // Number of menus
var usePictures = 0; // use pictures? 1 = yes, 0 = no

var ttls = new Array(); // An array for the title objects
var subs = new Array(); // An array for the submenu objects
var lastn;
var lastmove;

if (document.getElementById) {
visible = 'visible';
hidden = 'hidden';
}
for (var i = 1; i <= nom; i++) {
ttls = ('title' + i);
subs = ('submenu' +i);
}
function picopen(n) {
title = ('title' + n);
pic = ('pic' + n);
if (document.getElementById) {
document.getElementById(pic).src = "opened.gif";
}
}
function picclose(n) {
title = ('title' + n);
pic = ('pic' + n);
if (document.getElementById) {
document.getElementById(pic).src = "closed.gif";
}
}
lastn = (nom + 1);
lastmove = 0;
function lasttoggle(n,move) {
if (n <= nom) {
menu = ('submenu' + n);
if (document.getElementById) {
submenu = document.getElementById(menu).style;
}
if (submenu.visibility == visible) {
submenu.visibility = hidden;
for (var i = (n+1); i <= nom; i++) {
if (document.getElementById) {
document.getElementById(ttls).style.pixelTop -= move;
document.getElementById(subs).style.pixelTop -= move;
}
}
}
}
}
function toggle(n,move) {
menu = ('submenu' + n);
if (document.getElementById) {
submenu = document.getElementById(menu).style;
}
if (submenu.visibility == visible) {
submenu.visibility = hidden;
if (usePictures) picclose(n);
for (var i = (n+1); i <= nom; i++) {
if (document.getElementById) {
document.getElementById(ttls).style.pixelTop -= move;
document.getElementById(subs).style.pixelTop -= move;
}
}
}
else {
submenu.visibility = visible;
if (usePictures) picopen(n);
if (lastn != n) {
lasttoggle(lastn,lastmove);
}
for (var i = (n+1); i <= nom; i++) {
if (document.getElementById) {
document.getElementById(ttls).style.pixelTop += move;
document.getElementById(subs).style.pixelTop += move;
}
}
}
lastn = n;
lastmove = move;
}


function loadFrames(frame1,page1) {
eval("parent."+frame1+".location='"+page1+"'");
}


// End -->





</script>

The drop downs works, but I have the problem of the menu not making
room... maybe I'm barking up the wrong tree, so here's and example of
the code for one of the submenus:

<div class="title" id="title3" style="top: 40px;">
<a href="alumavault/index.html" Target=main onclick="javascript:
toggle(3,80); loadFrames('main', 'alumavault/index.html'); return
false">
Curved Metal Ceilings</a>
</div>

<div class="submenu" id="submenu3" style="top: 55px; height:48">
<a href="alumavault/av1000.html" target="main">System
1000/2000</a><br>
<a href="alumavault/av3000.html" target="main">System 3000</a><br>
<a href="alumavault/perimeters.html" target="main">Perimeters</a><br>
<a href="alumavault/options.html" target="main">Options</a><br>
<a href="alumavault/details.htm" target="main">Product Data</a><br>
<a href="Gallerybackup/gallery.htm#curved metal ceilings"
target="main">Gallery</a>
</div>


Basically, the beginning part is supposed to toggle the menu a specific
number of pixels... so if the menu isn't toggling, maybe the JS isn't
the problem, but the submenu code...
 
V

VK

Bryan said:
Like I said, I can get the menus to appear in Netscape using
"document.getElementById" instead of "document.all", but the rest of
the menu won't drop down to accommodate it. Everything I'm seeing tells
me to use "document.getElementById", and I do, but nothing tells me how
to make the menu move for the visible layer.

Are the "document.layers" entries necessary? Maybe they're
interfering...

Yes they are (besides other things). This is an ancient wartime script
for Netscape 4.x (document.layers) and Internet Explorer (document.all)

Netscape 4.x is gone, but as a reminder document.layers are still
supported (or at least do not return undefined > false) on some
browsers, for example Netscape (what a surprise!) or Safari.

Netscape 8.x adds even more spice in this picture because it operates
in so-called dual mode: depending on the page/script details it uses
either Gesko engine or Internet Explorer engine. As your ... if
(document.layers)... check comes before ...(document.all)... check -
the results may be really interesting but definitely not the expected
ones.

Internet Explorer still supports document.all collection so it works.
But all modern desktop browsers support document.getElementById method
including Internet Explorer.

It is really easier to right a new script from the scratch (will be
much less typing to do). But if you want to base on this code then:

1. Remove all "if (document.layers) {...}" branches completely
2. Replace all "else if (document.all)" with "if
(document.getElementById)"
3. Go through the text and replace all "document.all(" strings with
"document.getElementById("
4. Save and test.
 
B

Bryan

Invalid markup, insufficient feature tests, proprietary referencing,
evil[tm] eval(), and nonsensical, potential harmful comments (in that
order) ... what do you expect?

If I knew, I wouldn't need to ask... :p

I removed all comments, got rid of eval, now I just need to know what
part of the markup is invalid, what is "proprietary referencing", and
what feature tests I should run...

It still runs perfectly in IE, but the menu doesn't budge in Netscape
when the submenu is made visible...

Code now looks like:

<SCRIPT LANGUAGE="JavaScript">



var nom = 10;
var usePictures = 0;

var ttls = new Array();
var subs = new Array();
var lastn;
var lastmove;

if (document.getElementById) {
visible = 'visible';
hidden = 'hidden';
}
for (var i = 1; i <= nom; i++) {
ttls = ('title' + i);
subs = ('submenu' +i);
}
function picopen(n) {
title = ('title' + n);
pic = ('pic' + n);
if (document.getElementById) {
document.getElementById(pic).src = "opened.gif";
}
}
function picclose(n) {
title = ('title' + n);
pic = ('pic' + n);
if (document.getElementById) {
document.getElementById(pic).src = "closed.gif";
}
}
lastn = (nom + 1);
lastmove = 0;
function lasttoggle(n,move) {
if (n <= nom) {
menu = ('submenu' + n);
if (document.getElementById) {
submenu = document.getElementById(menu).style;
}
if (submenu.visibility == visible) {
submenu.visibility = hidden;
for (var i = (n+1); i <= nom; i++) {
if (document.getElementById) {
document.getElementById(ttls).style.pixelTop -= move;
document.getElementById(subs).style.pixelTop -= move;
}
}
}
}
}
function toggle(n,move) {
menu = ('submenu' + n);
if (document.getElementById) {
submenu = document.getElementById(menu).style;
}
if (submenu.visibility == visible) {
submenu.visibility = hidden;
if (usePictures) picclose(n);
for (var i = (n+1); i <= nom; i++) {
if (document.getElementById) {
document.getElementById(ttls).style.pixelTop -= move;
document.getElementById(subs).style.pixelTop -= move;
}
}
}
else {
submenu.visibility = visible;
if (usePictures) picopen(n);
if (lastn != n) {
lasttoggle(lastn,lastmove);
}
for (var i = (n+1); i <= nom; i++) {
if (document.getElementById) {
document.getElementById(ttls).style.pixelTop += move;
document.getElementById(subs).style.pixelTop += move;
}
}
}
lastn = n;
lastmove = move;
}



// End -->





</script>
 
T

Thomas 'PointedEars' Lahn

Bryan said:
Invalid markup, insufficient feature tests, proprietary referencing,
evil[tm] eval(), and nonsensical, potential harmful comments (in that
order) ... what do you expect?

If I knew, I wouldn't need to ask... :p

If you knew, you would throw it away. One important question just
occured to me: Does your "JS menu" still work if there is no JS support,
i.e. can one still navigate then? If not, you have certainly bigger
problems than a not-working script.
I removed all comments, got rid of eval, now I just need to know what
part of the markup is invalid,

what is "proprietary referencing", and

In contrast to `document.getElementById', `document.all' is not a
reference to an Function object, so it is inappropriate to refer
to its members by using the Call Operator (`()'). While the IE DOM
allows both document.all[...] and document.all(...) to refer to
members of this collection, this is certainly not standards compliant
behavior and therefore error-prone (MS may recognize this bug and
remove it in future IE versions with you having to deal with the
consequences): the same property cannot be both non-function and
function.
what feature tests I should run...

Opera also supports `document.all' for a targeted, yet failed,
compatibility to IE (for reasons that elude me), but does not provide
the same feature set as IE does. When it comes to AOM/DOM, you should
to test for all possible incompatible features you use, else you risk
(the user receiving) script errors.
It still runs perfectly in IE, [...]

Of course. To put in bluntly, IE can make gold out of the worst junk.
That is good for users in the short-term because they do not have to
deal with the results of the work of incompetent authors. It is bad
for both the user and the authors in the mid-term because average code
quality decreases and Web sites become less interoperable, while users
and therefore authors become dependent on the products and mercy of
one single vendor -- Micro$~1 Corp.


PointedEars

P.S.
Please learn how to quote properly:
<http://jibbering.com/faq/faq_notes/pots1.html> pp.
 
R

Richard Cornford

VK wrote:
Netscape 4.x is gone, but as a reminder document.layers are still
supported (or at least do not return undefined > false) on some
browsers, for example Netscape (what a surprise!) or Safari.

This statement is false. Anyone surprised by that?

1. Remove all "if (document.layers) {...}" branches completely
2. Replace all "else if (document.all)" with "if
(document.getElementById)"
3. Go through the text and replace all "document.all("
strings with "document.getElementById("
4. Save and test.

Advice best considered with an eye to the veracity of the statement
above.

Richard.
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
In contrast to `document.getElementById', `document.all' is not a
reference to an Function object, so it is inappropriate to refer
to its members by using the Call Operator (`()').

Since the only official specification of document.all is the MSDN
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/collections/all.asp>
and it uses function call as the access method, I don't think you
can say that it isn't. It's untraditional to use function objects
for collections, but not illegal in any way.
While the IE DOM allows both document.all[...] and document.all(...)
to refer to members of this collection, this is certainly not
standards compliant behavior

It can't be, since there is no standard. The document.all
functionality is very much a de-facto standard of "doing like IE
does".
and therefore error-prone (MS may recognize this bug and remove it
in future IE versions with you having to deal with the
consequences):

Unlikely. At times I wish they would forget about backwards
compatability and do things right instead, but it's not the way they
work.
the same property cannot be both non-function and function.

Then it's a function (i.e., an object with a [[Call]] method).
Functions are objects, so it's not impossible to have properties
that work with the property access operator at the same time.

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Unlikely.

I have seen it snow in June.
At times I wish they would forget about backwards compatability
and do things right instead, but it's not the way they work.

Upcoming IE7 may present a change in that policy. Hopefully.
the same property cannot be both non-function and function.

Then it's a function (i.e., an object with a [[Call]] method).
Functions are objects, so it's not impossible to have properties
that work with the property access operator at the same time.

But if it is indeed a function, should it then not have all properties
of Function objects and what is if a member is named like a property of
Function objects? This all still seems very strange to me.


PointedEars
 
R

RobG

Bryan said:
Invalid markup, insufficient feature tests, proprietary referencing,
evil[tm] eval(), and nonsensical, potential harmful comments (in that
order) ... what do you expect?

If I knew, I wouldn't need to ask... :p

I removed all comments, got rid of eval, now I just need to know what
part of the markup is invalid, what is "proprietary referencing", and
what feature tests I should run...

It still runs perfectly in IE, but the menu doesn't budge in Netscape
when the submenu is made visible...
http://www.alistapart.com/articles/horizdropdowns/



Code now looks like:

<SCRIPT LANGUAGE="JavaScript">

The language attribute is deprecated, type is required:

<script type="text/javascript">


Ta Tom :)

[...]

The rest of your script is amazingly complex for something so simple.
Loading the href into the main frame is achieved with a target
attribute, so the 'loadframe' function seems redundant and makes your
page unnecessarily reliant on scripting.

Try the following - it works whether scripting is enabled or not (links
have been truncated to '#' and target attributes removed for the sake of
the simplicity). It needs further work before commercial use but shows
an alternative approach that doesn't require any in-page scripting: it
is dependent on the page structure and the use of matching 'titlex' and
'submenux' ids numbered consecutively from 1.

I've moved the submenu divs inside the title divs and allowed the
document flow to place elements. Use CSS to adjust their position, not
absolute positioning.

Feature testing is done up-front - if scripting or suitable features
aren't supported, the menus are shown in full (i.e. your site remains
fully functional).


<html>
<head>
<title>Loopy stuff</title>
<style type="text/css">
..submenu{
padding-left: 20px;
}
</style>

<script type="text/javascript">

var smRefs = [];

function toggleSubs(){
var div = this.parentNode;
while (div.parentNode && 'div' != div.nodeName.toLowerCase()){
div = div.parentNode;
}
var num = div.id.replace(/title/,'');
for (var i=0, len=smRefs.length; i<len; ++i){
if (i != num && smRefs) smRefs.style.display = 'none';
}
smRefs[num].style.display =
('none' == smRefs[num].style.display)? '':'none';
}

function registerMenus(){

var body = document.body || document.documentElement;

// Do appropriate feature tests
if (!document.getElementById || !body || !body.style) return;

var i=0, el;
while ((el = document.getElementById('title' + ++i))){
smRefs = document.getElementById('submenu' + i)
smRefs.style.display = 'none';
var firstA = el.firstChild;
while ('a' != firstA.nodeName.toLowerCase()){
firstA = firstA.nextSibling;
}
firstA.onclick = toggleSubs;
}
}

</script>
</head>
<body onload="registerMenus();">

<div class="title" id="title1">
<a href="#">Column Covers</a>
<div class="submenu" id="submenu1">
<a href="#">Product Data</a><br>
<a href="#">Gallery</a>
</div>
</div>

<div class="title" id="title2">
<a href="#">Ceiling Specialties</a>
<div class="submenu" id="submenu2">
<a href="#">Perimeter Pockets</a><br>
<a href="#">Column Collars</a><br>
<a href="#">Partition Closures</a><br>
<a href="#">Baffle Ceilings</a><br>
</div>
</div>

</body>
</html>
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 11/15/2005 5:38 PM:
Bryan wrote:

<SCRIPT LANGUAGE="JavaScript">
[...]
else if (document.all) {
document.all(ttls).style.pixelTop -= move;
document.all(subs).style.pixelTop -= move;
[...]
function loadFrames(frame1,page1) {
eval("parent."+frame1+".location='"+page1+"'");
}


// End -->
[...]
</script>

So far all I can find concerning this script is people that are having
problems with it... maybe it's time for a new script, but I really
don't want to have to rebuild the menu...



Invalid markup, insufficient feature tests, proprietary referencing,
evil[tm] eval(), and nonsensical, potential harmful comments (in that
order) ... what do you expect?


Perhaps someone to lead him in the right direction other than the FAQ?
Maybe, as David did, give some useful advice?
 
L

Lasse Reichstein Nielsen

[document.all]
But if it is indeed a function, should it then not have all properties
of Function objects and what is if a member is named like a property of
Function objects? This all still seems very strange to me.

It's a host object, so it doesn't have to follow the conventions of
language objects. An object is a function if it has the internal
[[Call]] method, it doesn't have to inherit from Function.prototype
(ECMA262 section 8.6.2). And indeed it doesn't ("document.all.apply"
is undefined, and "document.all instanceof Function" is false).
The value of "typeof document.all" is "object", but typeof on host
objects is also allowed to be implementation dependent.

But document.all doesn't violate any rules of ECMAScript for a host
object.

/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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top