Forum Moderators: open

Message Too Old, No Replies

onmouseover hide/show description block: cross Browser problem

         

aotash

6:21 am on Feb 14, 2008 (gmt 0)

10+ Year Member



This javascript shows and hides description (like alt in image tag). It works in IE but not in Mozilla, what is wrong please help:

<SCRIPT LANGUAGE="JavaScript">

// ############## SIMPLE BROWSER SNIFFER
if (document.layers) {navigator.family = "nn4"}
if (document.all) {navigator.family = "ie4"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {navigator.family = "gecko"}

// ######### popup text
descarray = new Array(
"This site has some of the greatest scripts around!"
);

overdiv="0";
// ######### CREATES POP UP BOXES
function popLayer(a){
if(!descarray[a]){descarray[a]="<font color=red>This popup (#"+a+") isn't setup correctly - needs description</font>";}
if (navigator.family == "gecko") {pad="0"; bord="1 bordercolor=black";}
else {pad="1"; bord="0";}
desc = "<table cellspacing=0 cellpadding="+pad+" border="+bord+" bgcolor=000000><tr><td>\n"
+"<table cellspacing=0 cellpadding=3 border=0 width=100%><tr><td bgcolor=ffffdd><center><font size=-1>\n"
+descarray[a]
+"\n</td></tr></table>\n"
+"</td></tr></table>";
if(navigator.family =="nn4") {
document.object1.document.write(desc);
document.object1.document.close();
document.object1.left=x+15;
document.object1.top=y-5;
}
else if(navigator.family =="ie4"){
object1.innerHTML=desc;
object1.style.pixelLeft=x+15;
object1.style.pixelTop=y-5;
}
else if(navigator.family =="gecko"){
document.getElementById("object1").innerHTML=desc;
document.getElementById("object1").style.left=x+15;
document.getElementById("object1").style.top=y-5;
}
}
function hideLayer(){
if (overdiv == "0") {
if(navigator.family =="nn4") {eval(document.object1.top="-500");}
else if(navigator.family =="ie4"){object1.innerHTML="";}
else if(navigator.family =="gecko") {document.getElementById("object1").style.top="-500";}
}
}

// ######## TRACKS MOUSE POSITION FOR POPUP PLACEMENT
var isNav = (navigator.appName.indexOf("Netscape") !=-1);
function handlerMM(e){
x = (isNav) ? e.pageX : event.clientX + document.body.scrollLeft;
y = (isNav) ? e.pageY : event.clientY + document.body.scrollTop;
}
if (isNav){document.captureEvents(Event.MOUSEMOVE);}
document.onmousemove = handlerMM;
// End -->
</script>

==========================================
and body:

<div id="object1" style="position:absolute; background-color:FFFFDD;color:black;border-color:black;border-width:20; visibility:show; left:25px; top:-100px; z-index:+1" onmouseover="overdiv=1;" onmouseout="overdiv=0; setTimeout('hideLayer()',1000)">
pop up description layer
</div>

<a href="#" onMouseOver="popLayer(0)" onMouseOut="hideLayer()"><font size=-1 face=arial><b>THE JavaScript Source</b></font></a>

fside

3:46 pm on Feb 14, 2008 (gmt 0)

10+ Year Member



Your script seemed to run fine in NN4.7, NN6, etc. But nobody uses those, anymore. And you had to make a lot of concessions to the ancient NN4.7, particularly with the "desc" variable, and some other things.

I kept all of those concessions. Your script seemed to run fine. This seems to do the same. I'd suggest dropping NN4.7, NN6, etc support, and use style sheets instead of FONT, and modify attributes programmatically, and some other things. Here, I just hid the variables from the global object with an anonymous function, and used only a new Popup object. This works as far back as NN4.7, as you can see, though I don't know anyone was using these sort of patterns way back when (I certainly wasn't).

[fixed]
<html>
<head>

<SCRIPT LANGUAGE="JavaScript">

var Popup = { };

(function(){

/* Ceased with NN6 */
var isNN4 = !(!document.layers);

/* popup test */
var strTest = "This site has some of the greatest scripts around!";
var descarray = isNN4 ? new Array(strTest) : [ strTest ];

var overdiv= false;
var pad = "1", bord="0";

Popup.popLayer = function(a){

if ( !descarray[a] ){
descarray[a] ="<font color=red>This popup (#"+a+") isn't setup correctly - needs description</font>";
}

if (document.getElementById) {
pad ="0";
bord="1 bordercolor=black";
}

/* forced to use new, here */
var arDesc = new Array(
"<table cellspacing=\"0\" cellpadding=\"",
pad,
"\" border=\"",
bord,
"\" bgcolor=\"#000000\"><tr><td>",
"<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"100%\"><tr><td bgcolor=\"#ffffdd\">",
descarray[a],
"</td></tr></table>",
"</td></tr></table>").join("");

if( isNN4) {
document.object1.document.write(arDesc);
document.object1.document.close();
document.object1.left=x+15;
document.object1.top=y-5;

} else if (document.all){
object1.innerHTML=arDesc;
object1.style.pixelLeft=x+15;
object1.style.pixelTop=y-5;

} else {
var obj1 = document.getElementById("object1");
obj1.innerHTML=arDesc;
obj1.style.left=x+15;
obj1.style.top=y-5;
}

}

Popup.hideLayer = function(){
if ( !overdiv) {
if( isNN4 ) {
eval(document.object1.top="-500");
} else if (document.all) {
object1.innerHTML="";
} else {
document.getElementById("object1").style.top="-500";
}
}
}

Popup.out = function(){
overdiv =false;
setTimeout('Popup.hideLayer()',1000);
}

Popup.over = function(){
overdiv =true;
}

function handlerMM(e){
x = (e ===undefined) ? event.clientX +document.body.scrollLeft : e.pageX;
y = (e ===undefined) ? event.clientY +document.body.scrollTop : e.pageY;
}
if (document.captureEvents){ document.captureEvents(Event.MOUSEMOVE); }
document.onmousemove = handlerMM;

})();

// -->
</script>

</head>
</body>

<p>
Something, something here.
</p>

<p>
Something,
<a href="#" onMouseOver="Popup.popLayer(0)" onMouseOut="Popup.hideLayer()"><font size=-1 face=arial><b>something</b></font></a>, there.
</p>

<div id="object1" style="position:absolute; background-color:FFFFDD;color:black;border-color:black;border-width:20; visibility:show; left:25px; top:-100px; z-index:+1" onmouseover="Popup.over()" onmouseout="Popup.out()">
pop up description layer
</div>

</body>
</html>

[/fixed]

fside

3:57 pm on Feb 14, 2008 (gmt 0)

10+ Year Member



I just noticed, too, that x and y would have to be declared inside, and pad, etc, moved. And the over/out logic might be changed or even dropped, etc. But there's the idea.