...

View Full Version : Script not working on NN6



chelvis
04-13-2005, 06:10 PM
Here is a javascript which works in IE and NN4.7. But its not working in NN6. I wrote that else statement to handle NN6 with getElementById. Can some one check?

<SCRIPT LANGUAGE="javascript">

//global variables

var layerRef="null",styleSwitch="null";
var layerShowing="layer1";

function init(){
if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 6) {
layerRef="document.layers";
styleSwitch="";
}else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >=6) {
layerRef="document.getElementById()";
styleSwitch=".style";

}else {
layerRef="document.all";
styleSwitch=".style";
}

}
function changeDisplay(choice){
//don't do anything if no option was selected
hideLayer(layerShowing);
if (choice == "Compendex"){
var layerToShow="layer1";
}else if (choice == "All"){
var layerToShow="layer2";
}else if (choice == "Inspec"){
var layerToShow="layer3";
}else if (choice == "NTIS"){
var layerToShow="layer4";
}else if (choice == "Patents"){
var layerToShow="layer5";
}
showLayer(layerToShow);
layerShowing=layerToShow;
}

function showLayer(layerName){
eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
}
function hideLayer(layerName){
eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="hidden"');
}

</script>

hemebond
04-13-2005, 10:11 PM
Obviously I can't test, due to not having all your code, and this is a really bad way to do things, but I've made a couple of small changes.
<script type="text/javascript">
//global variables

var layerRef = '';
var styleSwitch = '';
var layerShowing = 'layer1';

function init()
{
if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 6)
{
layerRef = "document.layers";
styleSwitch = "";
}
else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >=6)
{
layerRef = "document.getElementById";
styleSwitch = ".style";
}
else
{
layerRef = "document.all";
styleSwitch = ".style";
}
}

function changeDisplay(choice)
{
//don't do anything if no option was selected
hideLayer(layerShowing);

switch(choice)
{
case 'Compendex':
var layerToShow = 'layer1';
break;
case 'All':
var layerToShow = 'layer2';
break;
case 'Inspec':
var layerToShow = 'layer3';
break;
case 'NTIS':
var layerToShow = 'layer4';
break;
case 'Patents':
var layerToShow = 'layer5';
break;
}
showLayer(layerToShow);
layerShowing = layerToShow;
}

function showLayer(layerName)
{
eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
}

function hideLayer(layerName)
{
eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="hidden"');
}
</script>

chelvis
04-14-2005, 02:09 AM
Thanks so much hemebond for tidying up the code. So after your post is it ok to use that code or its still a bit bad code?

Also my question was when I was chekcing for the different browsers, for Netscape6 I saw a code in the web like:

document.getElementById("layer name").style.visibility="visible"

But what do you pass for the "layer name" according to my code?

Because if you see at the bottom of the code, we are using like
eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');

This is valid for IE and NN4.X But for NN6.X what should be the value for "layer name" in document.getElementById("layer name").style.visibility="visible"?

I think this is what causing it not to work in NN6.X

glenngv
04-14-2005, 04:01 AM
That is an old style of cross-browser scripting. Object detection is more appropriate in this case.

function getStyle(layerName){
if (document.getElementById){
return document.getElementById(layerName).style;
}
if (document.all){
return document.all[layerName].style;
}
if (document.layers){
return document.layers[layerName];
}
}

function showLayer(layerName)
{
getStyle(layerName).visibility="visible";
}

function hideLayer(layerName)
{
getStyle(layerName).visibility="hidden";
}
I advise you not to use eval as it is inefficient (http://www.codingforums.com/showthread.php?t=20143). Almost always, it can be achieved by using square bracket notation. See my sig for more info on it.

chelvis
04-14-2005, 04:09 PM
Thanks so much glenngv. I got it to work in all browsers :thumbsup:

Kor
04-14-2005, 04:27 PM
NS6 accept getElementById() method as well, so that, by short, a smart crossbrowser code, as glenngv briefly pointed ot, should not have more than

if(document.getElementById)// ie5+,NS6+, Moz and all the modern compatible browsers

else if(document.all)//IE4

else if(document.layers)//NS4

The problem is that when google, you may find a lot of old codes which have only IE4/NS4 or even worse, the sequence is not correct (getElementById() should be first, to avoid duplicate the actions) and, furthermore, I guess that else if should be the correct switcher as well to avoid that.

glenngv
04-15-2005, 03:00 AM
I guess that else if should be the correct switcher as well to avoid that.
I didn't use that in my case because I have the return statement inside the if block.

Kor
04-15-2005, 08:44 AM
aha. yes, sorry, I have not noticed that... You're right



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum