fritz_the_blank
asked on
DIV Tags and Cross Browser Compatibility
I am working on the following code that show/hides a DIV. It works fine in IE 6, but has problems in NS 4.x and NS 6.x. In the former, only the labels show up. In the latter nothing happens.
I would appreciate any feedback.
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function switchDiv(strDivName,bolVi sible){
//figure out what browser we are dealing with
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
//identify the element based on browser type
if (isNS4){
objElement = document.layers[strDivName ];
}else if (isIE4) {
objElement = document.all[strDivName];
}else if (isIE5 || isNS6) {
objElement = document.getElementById(st rDivName);
}
//set the visibility
if(!bolVisible){
objElement.style.visibilit y = "hidden";
} else {
objElement.style.visibilit y = "visible";
}
return;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<p><INPUT type="button" value="Show" name=ShowMe onClick="javascript:switch Div('myDiv ',true)">
<INPUT type="button" value="Hide" name=HideMe onClick="javascript:switch Div('myDiv ',false)"> </p>
<div id="myDiv" style="visibility=hidden">
Text1<INPUT type="text" name="text1"><br>
Text2<INPUT type="text" name="text2"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
<INPUT type="reset" value="Reset" id=reset1 name=reset1>
</div>
</FORM>
</BODY>
</HTML>
Fritz the Blank
I would appreciate any feedback.
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function switchDiv(strDivName,bolVi
//figure out what browser we are dealing with
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
//identify the element based on browser type
if (isNS4){
objElement = document.layers[strDivName
}else if (isIE4) {
objElement = document.all[strDivName];
}else if (isIE5 || isNS6) {
objElement = document.getElementById(st
}
//set the visibility
if(!bolVisible){
objElement.style.visibilit
} else {
objElement.style.visibilit
}
return;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<p><INPUT type="button" value="Show" name=ShowMe onClick="javascript:switch
<INPUT type="button" value="Hide" name=HideMe onClick="javascript:switch
<div id="myDiv" style="visibility=hidden">
Text1<INPUT type="text" name="text1"><br>
Text2<INPUT type="text" name="text2"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
<INPUT type="reset" value="Reset" id=reset1 name=reset1>
</div>
</FORM>
</BODY>
</HTML>
Fritz the Blank
Couple of thing in general though fritz:
1)You don't have to say onclick="javascript:myFunc tion()"
That is really unneccessary and totally redundant.
onclick="myFunction()" is just fine.
1)You don't have to say onclick="javascript:myFunc
That is really unneccessary and totally redundant.
onclick="myFunction()" is just fine.
ASKER
Okay, so I have modified the design so that the form issue goes away. This now works on IE as well as with NS 4.x. Still no joy with NS 6.x.
<html>
<head>
<script language="javascript">
function divVisibility(id,vis)
{var dddd;
if(document.all)
{dddd=document.all[id].sty le;
dddd.visibility = vis ? "visible" : "hidden";
}
if(document.layers)
{dddd=document.layers[id];
dddd.visibility = vis ? "show" : "hide";
}
}
function showDiv(strDiv){
var objDiv=null;
if(document.all){
objDiv = eval("window." + strDiv);
}else{
objDiv = document.layers[strDiv];
}
if(document.all){
objDiv.style.visibility = "visible";
}else {
objDiv.visibility = "show";
}
}
function hideDiv(strDiv){
var objDiv=null;
if(document.all){
objDiv = eval("window." + strDiv);
}else{
objDiv = document.layers[strDiv];
}
if(document.all){
objDiv.style.visibility = "hidden";
}else {
objDiv.visibility = "hide";
}
}
</script>
</head>
<body>
<form>
<INPUT type="button" value="Show" name=button1 onClick="javascript:divVis ibility('D iv1',true) ">
<INPUT type="button" value="Hide" name=button2 onClick="javascript:divVis ibility('D iv1',false )"><p>
</form>
<div id="Div1" name="Div1" style="visibility:hidden;p osition:re lative">
<FORM action="" method=POST id=form1 name=form1>
Text1<INPUT type="text" name="text1"><br>
Text2<INPUT type="text" name="text2"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
<INPUT type="reset" value="Reset" id=reset1 name=reset1>
</FORM>
</div>
</body>
</html>
Fritz the Blank
<html>
<head>
<script language="javascript">
function divVisibility(id,vis)
{var dddd;
if(document.all)
{dddd=document.all[id].sty
dddd.visibility = vis ? "visible" : "hidden";
}
if(document.layers)
{dddd=document.layers[id];
dddd.visibility = vis ? "show" : "hide";
}
}
function showDiv(strDiv){
var objDiv=null;
if(document.all){
objDiv = eval("window." + strDiv);
}else{
objDiv = document.layers[strDiv];
}
if(document.all){
objDiv.style.visibility = "visible";
}else {
objDiv.visibility = "show";
}
}
function hideDiv(strDiv){
var objDiv=null;
if(document.all){
objDiv = eval("window." + strDiv);
}else{
objDiv = document.layers[strDiv];
}
if(document.all){
objDiv.style.visibility = "hidden";
}else {
objDiv.visibility = "hide";
}
}
</script>
</head>
<body>
<form>
<INPUT type="button" value="Show" name=button1 onClick="javascript:divVis
<INPUT type="button" value="Hide" name=button2 onClick="javascript:divVis
</form>
<div id="Div1" name="Div1" style="visibility:hidden;p
<FORM action="" method=POST id=form1 name=form1>
Text1<INPUT type="text" name="text1"><br>
Text2<INPUT type="text" name="text2"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
<INPUT type="reset" value="Reset" id=reset1 name=reset1>
</FORM>
</div>
</body>
</html>
Fritz the Blank
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
let's go back to original code(with the form issue changed for NS4.x):
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
function switchDiv(strDivName,bolVi sible){
//identify the element based on browser type
if (isNS4) {
objElement = document.layers[strDivName ];
} else if (isIE4) {
objElement = document.all[strDivName];
} else if (isIE5 || isNS6) {
objElement = document.getElementById(st rDivName);
}
if(!bolVisible) {
objElement.style.visibilit y = "hidden";
} else {
objElement.style.visibilit y = "visible";
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<p><INPUT type="button" value="Show" name=ShowMe onClick="switchDiv('myDiv' ,true)">
<INPUT type="button" value="Hide" name=HideMe onClick="switchDiv('myDiv' ,false)">< /p>
</form>
<form>
<div id="myDiv" style="visibility:hidden;p osition:re lative">
Text1<INPUT type="text" name="text1"><br>
Text2<INPUT type="text" name="text2"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
<INPUT type="reset" value="Reset" id=reset1 name=reset1>
</div>
</FORM>
</BODY>
</HTML>
What I'm saying is that this code above works on Mozilla 1(and therfore Netscape 7). So there's either some bug in Netscape 6(very possible) or something.Run that code and then press the buttons(hide and show) then type
javascript:
into the address bar(erasing the old text first) and press Enter like i said. Some errors may come up on the javascript console.
I can't do it because i don't have Netscape 6- and it doesn't throw errors on Moz 1(Netscape 7).
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
function switchDiv(strDivName,bolVi
//identify the element based on browser type
if (isNS4) {
objElement = document.layers[strDivName
} else if (isIE4) {
objElement = document.all[strDivName];
} else if (isIE5 || isNS6) {
objElement = document.getElementById(st
}
if(!bolVisible) {
objElement.style.visibilit
} else {
objElement.style.visibilit
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<p><INPUT type="button" value="Show" name=ShowMe onClick="switchDiv('myDiv'
<INPUT type="button" value="Hide" name=HideMe onClick="switchDiv('myDiv'
</form>
<form>
<div id="myDiv" style="visibility:hidden;p
Text1<INPUT type="text" name="text1"><br>
Text2<INPUT type="text" name="text2"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
<INPUT type="reset" value="Reset" id=reset1 name=reset1>
</div>
</FORM>
</BODY>
</HTML>
What I'm saying is that this code above works on Mozilla 1(and therfore Netscape 7). So there's either some bug in Netscape 6(very possible) or something.Run that code and then press the buttons(hide and show) then type
javascript:
into the address bar(erasing the old text first) and press Enter like i said. Some errors may come up on the javascript console.
I can't do it because i don't have Netscape 6- and it doesn't throw errors on Moz 1(Netscape 7).
ASKER
This is very strange...now it works like a charm!
What's your take regarding IE 4.x? Are any further modifications necessary?
Fritz the Blank
What's your take regarding IE 4.x? Are any further modifications necessary?
Fritz the Blank
ASKER
Hold on, there is an error in NS 4 now:
objElement.style has no properties.
JavaScript Error: file:/C|/Documents and
Settings/Patrick/Desktop/D ivHideShow .htm, line 24:
objElement.style has no properties.
objElement.style has no properties.
JavaScript Error: file:/C|/Documents and
Settings/Patrick/Desktop/D
objElement.style has no properties.
ASKER
This works on all three:
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
function switchDiv(strDivName,bolVi sible){
//identify the element based on browser type
if (isNS4) {
objElement = document.layers[strDivName ];
} else if (isIE4) {
objElement = document.all[strDivName];
} else if (isIE5 || isNS6) {
objElement = document.getElementById(st rDivName);
}
if(isNS4){
if(!bolVisible) {
//objElement.style.visibil ity = "hidden";
objElement.visibility ="hidden"
} else {
//objElement.style.visibil ity = "visible";
objElement.visibility ="visible"
}
}else{
if(!bolVisible) {
objElement.style.visibilit y = "hidden";
//document.layers[strDivNa me].visibi lity ="hidden"
} else {
objElement.style.visibilit y = "visible";
//document.layers[strDivNa me].visibi lity ="visible"
}
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<p><INPUT type="button" value="Show" name=ShowMe onClick="switchDiv('myDiv' ,true)">
<INPUT type="button" value="Hide" name=HideMe onClick="switchDiv('myDiv' ,false)">< /p>
</form>
<div id="myDiv" style="visibility:hidden;p osition:re lative">
<form>
Text1<INPUT type="text" name="text1"><br>
Text2<INPUT type="text" name="text2"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
<INPUT type="reset" value="Reset" id=reset1 name=reset1>
</FORM>
</div>
</BODY>
</HTML>
So what about IE 4.x?
Fritz the Blank
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
function switchDiv(strDivName,bolVi
//identify the element based on browser type
if (isNS4) {
objElement = document.layers[strDivName
} else if (isIE4) {
objElement = document.all[strDivName];
} else if (isIE5 || isNS6) {
objElement = document.getElementById(st
}
if(isNS4){
if(!bolVisible) {
//objElement.style.visibil
objElement.visibility ="hidden"
} else {
//objElement.style.visibil
objElement.visibility ="visible"
}
}else{
if(!bolVisible) {
objElement.style.visibilit
//document.layers[strDivNa
} else {
objElement.style.visibilit
//document.layers[strDivNa
}
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<p><INPUT type="button" value="Show" name=ShowMe onClick="switchDiv('myDiv'
<INPUT type="button" value="Hide" name=HideMe onClick="switchDiv('myDiv'
</form>
<div id="myDiv" style="visibility:hidden;p
<form>
Text1<INPUT type="text" name="text1"><br>
Text2<INPUT type="text" name="text2"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
<INPUT type="reset" value="Reset" id=reset1 name=reset1>
</FORM>
</div>
</BODY>
</HTML>
So what about IE 4.x?
Fritz the Blank
What about it? That'll be fine for IE. Is Netscape all fine now?
ASKER
So are you saying that IE 4.x handles div tags in the same way that IE 5.x plus does?
Fritz the Blank
Fritz the Blank
yup, that code works because it refernces the element using documant.all
The rest is the same:obElement.style.visib ility
The rest is the same:obElement.style.visib
ASKER
Thanks for the help!
Fritz the Blank
Fritz the Blank
Type "javascript:"
without the quotes(but with the colon into address bar and tell what error comes up.