Connecting Tech Pros Worldwide Forums | Help | Site Map

Highlighting element at mouse position - doesn't work in Firefox

Newbie
 
Join Date: Jun 2007
Posts: 15
#1: Jun 14 '07
i created some javascript DHTML that highlights which menu selection the mouse is currently at, but it won't work on Firefox. Any idea? The source is old, so probably things have changed.

Expand|Select|Wrap|Line Numbers
  1. <script language=JavaScript>
  2.  
  3. function showMenu(menuToShow)
  4. {
  5.       var srcElement = event.srcElement;
  6.       var xPos = parseInt(srcElement.offsetLeft);
  7.       var yPos = parseInt(srcElement.offsetTop);
  8.  
  9.       menuToShow.style.left = xPos + (srcElement.width)
  10.       menuToShow.style.top = yPos;
  11. }
  12.  
  13. function hideMenu(menuToHide)
  14. {
  15.  
  16.       if (event.toElement != menuToHide &&
  17.             menuToHide.contains(event.toElement) == false)
  18.       {
  19.             menuToHide.style.left = -200;
  20.             menuToHide.style.top = -1000;
  21.       }
  22. }
  23.  
  24. function document_onmouseover()
  25. {
  26.  
  27.       var srcElement = event.srcElement;
  28.  
  29.       if (srcElement.tagName == "TD" && typeof(srcElement.RollOver) != "undefined")
  30.       {
  31.             srcElement.style.color = "darkblue";
  32.             srcElement.style.backgroundColor = "E9ECF5";
  33.       }
  34. }
  35.  
  36. function document_onmouseout()
  37. {
  38.       var srcElement = event.srcElement;
  39.  
  40.       if (srcElement.tagName == "TD" && typeof(srcElement.RollOut) != "undefined")
  41.       {
  42.             srcElement.style.color = "E9ECF5";
  43.             srcElement.style.backgroundColor = "#AA0202";
  44.       }
  45. }
  46.  
  47. function goPage(src)
  48. {
  49.       window.location.href = src;
  50. }
  51.  
  52. document.onmouseover = document_onmouseover;
  53. document.onmouseout = document_onmouseout;
  54.  
  55. </script>
  56.  
  57. <style>
  58.       .DivMenu {position.absolute;
  59.             left:0;
  60.             top:0;
  61.             width:500;
  62.             background-color:#AA0202;
  63.                         }
  64.       .tdMenu {
  65.             color:#E9ECF5;
  66.             font-family:verdana;
  67.             font-size:100%;
  68.             cursor:default;
  69.             text-align:center;
  70.             }
  71.  
  72.  
  73. </style>
  74. </head>
  75.  
  76. <body>
  77.  
  78. <div id="MenuDiv" class="DivMenu" width="500" height="40" border="1" onmouseout="return hideMenu(this)">
  79. <table border=0 cellspacing=1 cellpadding=1 id="WoodTable">
  80.       <tr>
  81.             <td id="Home" width="100" RollOver RollOut
  82.             onclick="goPage('home.htm')"  class="tdMenu">Home
  83.             </td>
  84.  
  85.             <td id="Resume" width="100" RollOver RollOut
  86.             onclick="goPage('resume.htm')" class="tdMenu">Resume
  87.             </td>
  88.  
  89.             <td id="Courses" width="100" RollOver RollOut
  90.             onclick="goPage('courses.htm')"  class="tdMenu">Courses
  91.             </td>
  92.  
  93.             <td id="Cool Sites" width="100" RollOver RollOut
  94.             onclick="goPage('coolsites.htm')"  class="tdMenu">Cool Sites
  95.             </td>
  96.  
  97.             <td id="Blog" width="100" RollOver RollOut
  98.             onclick="goPage('blog.htm')"  class="tdMenu">Blog
  99.             </td></tr>
  100.  
  101. </table>
  102. </div>
  103.  
  104.  

shoonya's Avatar
Familiar Sight
 
Join Date: May 2007
Location: india
Posts: 159
#2: Jun 14 '07

re: Highlighting element at mouse position - doesn't work in Firefox


IE nd firefox/mozilla treats events diffrentlt
in IE event is a global object so you dont need to pass the event to event handler/function where as in mozilla you have to pass the event to the function

[HTML]document.onmousedown=dragHandler;

function dragHandler(e){
if (e == null) { e = window.event }
var target = e.target != null ? e.target : e.srcElement;
//alert(target.id)
....
...
}[/HTML]

similarly e.target nd e.srcElement works
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Jun 14 '07

re: Highlighting element at mouse position - doesn't work in Firefox


Changed thread title.
Reply