Connecting Tech Pros Worldwide Forums | Help | Site Map

IE, NS & Firerox Problem - DIVs, document.layers and getElementById

InvisibleMan
Guest
 
Posts: n/a
#1: Jul 23 '05
Thanks in Advance for any help on this - its truely sending my
head in loops... so I appreciate your efforts!

okay, I have a javascript listed below that drops down submenus
contained within:
<div class="small" style="display: none" id="menu1_menu">

when the heading is clicked:
<a href=.. onClick="expandDiv('menu1')">

- This works perfect in IE, NS6 (I Think), but not NS7 or
FireFox. I know my issues lay in the 'var Browsers = ' and
document.layers, getElementById. The menu is opened and set
using the function setMenu() called elsewhere... menu1 is the
default, but it does store a cookie if this changes to menu2,
menu3 etc...

Essentially i'd like this to work for all browsers - in fact its
essential it does :( - Please stop me from loosing anymore hair
- Regards Lee


var ua = navigator.userAgent;
var opera = /opera [56789]|opera\/[56789]/i.test(ua);
var ie = !opera && /msie [56789]/i.test(ua);
// preventing opera to be identified as ie
var moz = !opera && /mozilla\/[56789]/i.test(ua);
// preventing opera to be identified as mz

function getCookie(NameOfCookie)
{

if (document.cookie.length > 0)
{

begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
{

begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return "menu1";

}

function setCookie(NameOfCookie, value, expiredays)
{

var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 *
3600 * 1000));

document.cookie = NameOfCookie + "=" + escape(value) +
((expiredays == null) ? "" : "; expires=" +
ExpireDate.toGMTString());
}

function closeall()
{
var objs;

if (ie)
{
objs = document.all.tags("DIV");
}
else if (ua)
{
objs = document.getElementById("DIV");
}
else if (document.layers)
{
objs = document.layers["DIV"];
}
else
{
alert('Non supported browser');
}

for (var i=0; i<objs.length; i++) {
if (objs[i].className == "small")
{

objs[i].style.display = "none";
}
}
}

function expandDiv(tahw) {
setCookie("menu", tahw, 7);
closeall();

what = tahw + "_menu"

if (document.getElementById(what).style.display == "none") {
document.getElementById(what).style.display = "";
} else {
document.getElementById(what).style.display = "none";
}
}

function setmenu()
{
var menuvar;
menuvar = getCookie("menu");
expandDiv(menuvar);
}

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------


Fred Oz
Guest
 
Posts: n/a
#2: Jul 23 '05

re: IE, NS & Firerox Problem - DIVs, document.layers and getElementById


InvisibleMan wrote:
[...][color=blue]
> - This works perfect in IE, NS6 (I Think), but not NS7 or
> FireFox. I know my issues lay in the 'var Browsers = ' and
> document.layers, getElementById. The menu is opened and set[/color]

Then that's what I'll concentrate on fixing.
[color=blue]
> using the function setMenu() called elsewhere... menu1 is the
> default, but it does store a cookie if this changes to menu2,
> menu3 etc...
>
> Essentially i'd like this to work for all browsers - in fact its
> essential it does :( - Please stop me from loosing anymore hair
> - Regards Lee
>
>
> var ua = navigator.userAgent;
> var opera = /opera [56789]|opera\/[56789]/i.test(ua);
> var ie = !opera && /msie [56789]/i.test(ua);
> // preventing opera to be identified as ie
> var moz = !opera && /mozilla\/[56789]/i.test(ua);
> // preventing opera to be identified as mz[/color]
[...]

Get rid of the crappy browser detection. Try:

if (document.getElementById) {
objs = document.getElementById('DIV');
} else if (document.all) {
objs = document.all['DIV'];
} else if (document.layers) {
objs = document.all['DIV'];
}

...

[...][color=blue]
>
> for (var i=0; i<objs.length; i++) {[/color]

It is more efficient to get the length just once:

for (var i=0, var len=<objs.length; i<len; i++) {
...

[...][color=blue]
> if (document.getElementById(what).style.display == "none") {
> document.getElementById(what).style.display = "";
> } else {
> document.getElementById(what).style.display = "none";
> }
> }[/color]


And you'd better add feature detection (as above) here too, else the
non-getElementById browsers will fail.

That may not fix all your issues, but it should get you going again.

--
Fred
InvisibleMan
Guest
 
Posts: n/a
#3: Jul 23 '05

re: IE, NS & Firerox Problem - DIVs, document.layers and getElementById


Thanks for that okay, i've done the following but keep getting
errors... i'm getting what your trying to say... but its not
working and causing errors... Help

function closeall(){
var objs;

if (document.getElementById) {
objs = document.getElementById('DIV');
} else if (document.all) {
objs = document.all['DIV'];
} else if (document.layers) {
objs = document.all['DIV'];
} else {
alert('Non supported browser');
}
for (var i=0; i<objs.length; i++) {
if (objs[i].className == "small")
{
objs[i].style.display = "none";
}
}
}

AND

function expandDiv(tahw) {
setCookie("thingsmenu", tahw, 7);
closeall();

what = tahw + "_menu"

if (document.getElementById(what).style.display ==
"none") {
document.getElementById(what).style.display = "")
} else if (document.all) {
document.all['DIV'](what).style.display = "none");
} else if (document.layers) {
document.all['DIV'](what).style.display = "none");
}
else {
document.getElementById(what).style.display = "none";
}
}

----------------------------------------------
Posted with NewsLeecher v2.0 Beta 5
* Binary Usenet Leeching Made Easy
* http://www.newsleecher.com/?usenet
----------------------------------------------

Fred Oz
Guest
 
Posts: n/a
#4: Jul 23 '05

re: IE, NS & Firerox Problem - DIVs, document.layers and getElementById


InvisibleMan wrote:
[...][color=blue]
> if (document.getElementById) {
> objs = document.getElementById('DIV');[/color]

Sorry, I think what you are looking for is
document.getElementsByTagName:

if (document.getElementsByTagName) {
objs = document.getElementsByTagName('DIV');

and that will return a collection.

--
Fred


Closed Thread