473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Browser Detects - Layers, GetElement, All

Hi, Thanks for any help in advance

I'm using the following JS to drop/show content that is hidden:

function expandDiv(tahw) {

what = tahw + "_menu"

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

}

where:

<div class="LftNavDrop" id="LeftNav"><a href="javascript:
expandDiv('leftnav1')" onClick="javascript:
expandDiv('leftnav1')">Etiam Accumsan<br />&amp; Curabitur
Viverra</a></div>
<div class="LftNavDropped" style="display: none" id="leftnav1_menu">
Content...
</div>

(Which works in IE only)

But would really like for the JS to test conditions for ALL browsers
based on/or something similar to:
if (document.layers)
{ document.layers['DIV'];

else if (document.all)
{ document.all.tags("DIV"); alert('Works in IE');}

else if (document.all || document.layers)
{ document.all.tags("DIV");}

else if (document.getElementByTag && document.all)
{ document.getElementById("DIV");}

else if (document.getElementByTag && !document.all)
{ document.getElementById("DIV");}

else if (document.getElementByTag)
{ document.getElementById("DIV");}
else
{ alert('Sorry, One of our main navigation systems does not support
your browser.'); }

}
Please, can anyone help me fix this above script so that it will work?

Regards

Lee

Jul 23 '05 #1
2 5905
le*******@gmail.com wrote:
Hi, Thanks for any help in advance

I'm using the following JS to drop/show content that is hidden:

function expandDiv(tahw) {

what = tahw + "_menu"

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

}

where:

<div class="LftNavDrop" id="LeftNav"><a href="javascript:
expandDiv('leftnav1')" onClick="javascript:
expandDiv('leftnav1')">Etiam Accumsan<br />&amp; Curabitur
Viverra</a></div>
<div class="LftNavDropped" style="display: none" id="leftnav1_menu">
Content...
</div>

(Which works in IE only)

But would really like for the JS to test conditions for ALL browsers
based on/or something similar to:
if (document.layers)
{ document.layers['DIV'];

else if (document.all)
{ document.all.tags("DIV"); alert('Works in IE');}

else if (document.all || document.layers)
{ document.all.tags("DIV");}

else if (document.getElementByTag && document.all)
{ document.getElementById("DIV");}

else if (document.getElementByTag && !document.all)
{ document.getElementById("DIV");}

else if (document.getElementByTag)
{ document.getElementById("DIV");}
else
{ alert('Sorry, One of our main navigation systems does not support
your browser.'); }

}
Please, can anyone help me fix this above script so that it will work?


Have a browse of the group FAQ, particularly:

<URL:http://www.jibbering.com/faq/#FAQ4_15>

Check out the DynWrite stuff a very simple version is place the
following at the start of a script element outside any function:

if( !document.getElementById ){
if ( document.all ){
document.getElementById = function(id){return document.all[id];};
} else if ( document.layers ) {
document.getElementById = function(id){return document.layers[id];};
} else {
return null;
}
}

But that may not provide all the functionality you require. Also,
names and IDs must not clash and ID's must be unique in the page. The
above link warns:

"...document.all collection does not have exactly analogous behaviour
with the document.getElementById method. If multiple elements have
the same ID (or share an ID with the NAME of other elements) then
document.all returns a collection instead of an individual element,
while document.getElementById only ever returns an individual
element or null. However, ID attributes are supposed to be unique to
an HTML page if that page is valid HTML 4 so multiple identical IDs
should not be a problem. The issue with IDs coinciding with NAMEs
remains, though it would not be good HTML design to provoke that
problem."
The idea is to test only for browsers that don't support getElementById
(which by now must be a very small proportion of those in use) so that
those that do support it are left alone.
--
Rob
Jul 23 '05 #2
RobG wrote:
<snip>
if( !document.getElementById ){
if ( document.all ){
document.getElementById = function(id){
return document.all[id];
};
} else if ( document.layers ) {
document.getElementById = function(id){return
document.layers[id];}; } else {
return null;
}
}

<snip>

Part of the reason for creating an emulation of -
document.getElementById - is to avoid having to repeatedly feature
detect it prior to its use. You have created it so you know that it will
be in the environment from then on.

This has the advantage of providing an efficiency gain because you do
the feature detection only once, at the point of creating the emulation,
rather than at each call to the method.

However, in the above example, no document.getElementById - method is
created in the final - else - branch, so code that is not going to error
in calling the emulation is still going to have to feature detect the
method in order not to error-out when calling it. This is best avoided
by including a default:-

document.getElementById = function(){return null;};

- statement in the final branch for when - getElementById - is not
native, but no viable alternative is available. The values returned
from - getElementById - would need to be tested on a per-call bases, but
that should be true anyway as the original method may return null when
it cannot find the element in the DOM.

Richard.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simba | last post by:
In some pages of my website I use a code like the following: for (var n = 0; n < getTagsArray("SPAN").length; n++){ //SPAN is just an example. I also use other tags tag =...
11
by: Simon Wigzell | last post by:
I cobbled together the following function from examples on the internet to set named spanned items in the parent form. It works fine for IE but not at all for netscape. What other browser...
6
by: McKirahan | last post by:
Is this a very good browser check? <html> <head> <title>wB.htm</title> <script type="text/javascript"> var adBtype = "??"; function wB() { if (document.getElementById && !document.all) {...
8
by: lawrence | last post by:
Under the domain publicpen.com I've several dozen sites in subdiretories, such as www.publicpen.com/honenbeger. I've no trouble with any of these sites. But under one, which I put in yesterday,...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
3
by: cjl | last post by:
Hey all: I'm stuck. I'm using the code from www.quirksmode.org for cross browser access to HTML elements, but I'm getting a javascript error. The relevant code: function getObj(name) { if...
5
by: Trenqo 0 | last post by:
Instead of doing repetitive checks throughout my code, or defining new methods that won't work unless they are included, I have taken the approach of redefining existing methods in order to make...
4
by: cwdjrxyz | last post by:
Take a look at http://www.explorerdestroyer.com/ and view the script used to detect IE. It uses quite a maze of elaborate script that examines user agents and many properties of a browser. Of...
1
oranoos3000
by: oranoos3000 | last post by:
hi would you please help me i have a online shopping center that i show pictures of the my product in home page. in the InterExplorer pictures is shown correctly but in Firefox browser is shown...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.