473,409 Members | 2,056 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,409 software developers and data experts.

Mouse event capture failing in Firefox?

Mobius Evalon
I'm trying to make this thing work in Firefox, because I know the client my game is on is pondering switching to Gecko (they're currently using something very similar to IE4 for browser windows launched in the client). This tooltip script works fine in IE, and the coordinates are apparently being read and changed in Firefox (I tossed in a div for debug), but I keep getting an "ev has no properties" error in Firefox and the tooltip div fails to appear.

Following is the entirety of the document I'm working on:

Expand|Select|Wrap|Line Numbers
  1. <head>
  2.     <style type="text/css">
  3.         #tooltip_div
  4.         {
  5.             position: absolute;
  6.             visibility: hidden;
  7.             z-index: 20;
  8.             top: 0px;
  9.             left: 0px;            
  10.         }
  11.         .tooltip_table
  12.         {
  13.             border: 0px;
  14.             width: 250px;
  15.             height: 80px;
  16.             margin: 0px;
  17.             padding: 0px;
  18.             background-color: #404040;
  19.             background: url('table_bg.PNG');
  20.             color: #aaaaaa;
  21.         }
  22.         .tooltip_head
  23.         {
  24.             border: 0px;
  25.             text-align: center;
  26.             vertical-align: middle;
  27.             height: 10px;
  28.             font-family: verdana,sans-serif;
  29.             font-size: 10px;
  30.             background: url('title_bg.PNG');
  31.             background-color: #808080;
  32.             color: #aaaaaa;
  33.         }
  34.         .tooltip_content
  35.         {
  36.             border: 0px;
  37.             margin: 0px;
  38.             text-align: center;
  39.             vertical-align: middle;
  40.             font-family: verdana,sans-serif;
  41.             font-size: 10px;
  42.             color: #aaaaaa;
  43.         }
  44.     </style>
  45.     <script type="text/javascript">
  46.         if(!document.all) document.captureEvents(Event.MOUSEMOVE);
  47.         document.onmousemove = realign_tooltip;
  48.  
  49.         var body_width = 0;
  50.         var body_height = 0;
  51.         var tooltip_x_offset = 15;
  52.         var tooltip_y_offset = 15;
  53.         var tooltip_div;
  54.  
  55.         function init_tooltip()
  56.         {
  57.             if(document.all)
  58.             {
  59.                 body_width = document.body.clientWidth;
  60.                 body_height = document.body.clientHeight;
  61.             }
  62.             else
  63.             {
  64.                 body_width = window.innerWidth;
  65.                 body_height = window.innerHeight;
  66.             }
  67.         }
  68.         function show_tooltip(title,content)
  69.         {
  70.             tooltip_div = document.getElementById('tooltip_div');
  71.             tooltip_div.innerHTML = ('<table class="tooltip_table"><tr><td class="tooltip_head">'+title+'</td></tr><tr><td class="tooltip_content">'+content+'</td></tr></table>');
  72.             realign_tooltip();
  73.             tooltip_div.style.visibility = 'visible';
  74.         }
  75.         function hide_tooltip()
  76.         {
  77.             tooltip_div.style.visibility = 'hidden';
  78.             tooltip_div = null;
  79.         }
  80.         function realign_tooltip(ev)
  81.         {
  82.             ev = (ev || window.event);
  83.             init_tooltip();
  84.             if(tooltip_div)
  85.             {
  86.                 if(document.all)
  87.                 {
  88.                     tooltip_div.style.left = ((((ev.clientX+tooltip_x_offset+tooltip_div.clientWidth) <= body_width) ? (ev.clientX+tooltip_x_offset) : (body_width-tooltip_div.clientWidth))+'px');
  89.                     tooltip_div.style.top = ((((ev.clientY+tooltip_y_offset+tooltip_div.clientHeight) <= body_height) ? (ev.clientY+tooltip_y_offset) : (body_height-tooltip_div.clientHeight))+'px');
  90.                 }
  91.                 else
  92.                 {
  93.                     tooltip_div.style.left = ((((ev.pageX+tooltip_x_offset+tooltip_div.clientWidth) <= body_width) ? (ev.pageX+tooltip_x_offset) : (body_width-tooltip_div.clientWidth))+'px');
  94.                     tooltip_div.style.top = ((((ev.pageY+tooltip_y_offset+tooltip_div.clientHeight) <= body_height) ? (ev.pageY+tooltip_y_offset) : (body_height-tooltip_div.clientHeight))+'px');
  95.                 }
  96.                 document.getElementById('debug').innerHTML = (''+body_width+'x'+body_height+'<br>'+tooltip_div.style.left+', '+tooltip_div.style.top);
  97.             }
  98.         }
  99.     </script>
  100. </head>
  101. <body onload="init_tooltip();">
  102.     <a href="#" onmouseover="show_tooltip('Total mutes','This is the total number of automated mutes currently being applied. This number does not include mutes by staff or those who have logged out before an auto-mute expired.');" onmouseout="hide_tooltip();">blah</a>
  103.     <br><br><br><br><a href="#" onmouseover="show_tooltip('lol','More text');" onmouseout="hide_tooltip();">lol</a>
  104.     <br><br><br><br><div id="tooltip_div">&nbsp;</div>
  105.     <div id="debug">0,0</div>
Feb 25 '07 #1
2 5996
pronerd
392 Expert 256MB
I'm trying to make this thing work in Firefox, because I know the client my game is on is pondering switching to Gecko (they're currently using something very similar to IE4 for browser windows launched in the client). This tooltip script works fine in IE, and the coordinates are apparently being read and changed in Firefox (I tossed in a div for debug), but I keep getting an "ev has no properties" error in Firefox and the tooltip div fails to appear.

Following is the entirety of the document I'm working on:

Expand|Select|Wrap|Line Numbers
  1. <head>
  2.     <style type="text/css">
  3.         #tooltip_div
  4.         {
  5.             position: absolute;
  6.             visibility: hidden;
  7.             z-index: 20;
  8.             top: 0px;
  9.             left: 0px;            
  10.         }
  11.         .tooltip_table
  12.         {
  13.             border: 0px;
  14.             width: 250px;
  15.             height: 80px;
  16.             margin: 0px;
  17.             padding: 0px;
  18.             background-color: #404040;
  19.             background: url('table_bg.PNG');
  20.             color: #aaaaaa;
  21.         }
  22.         .tooltip_head
  23.         {
  24.             border: 0px;
  25.             text-align: center;
  26.             vertical-align: middle;
  27.             height: 10px;
  28.             font-family: verdana,sans-serif;
  29.             font-size: 10px;
  30.             background: url('title_bg.PNG');
  31.             background-color: #808080;
  32.             color: #aaaaaa;
  33.         }
  34.         .tooltip_content
  35.         {
  36.             border: 0px;
  37.             margin: 0px;
  38.             text-align: center;
  39.             vertical-align: middle;
  40.             font-family: verdana,sans-serif;
  41.             font-size: 10px;
  42.             color: #aaaaaa;
  43.         }
  44.     </style>
  45.     <script type="text/javascript">
  46.         if(!document.all) document.captureEvents(Event.MOUSEMOVE);
  47.         document.onmousemove = realign_tooltip;
  48.  
  49.         var body_width = 0;
  50.         var body_height = 0;
  51.         var tooltip_x_offset = 15;
  52.         var tooltip_y_offset = 15;
  53.         var tooltip_div;
  54.  
  55.         function init_tooltip()
  56.         {
  57.             if(document.all)
  58.             {
  59.                 body_width = document.body.clientWidth;
  60.                 body_height = document.body.clientHeight;
  61.             }
  62.             else
  63.             {
  64.                 body_width = window.innerWidth;
  65.                 body_height = window.innerHeight;
  66.             }
  67.         }
  68.         function show_tooltip(title,content)
  69.         {
  70.             tooltip_div = document.getElementById('tooltip_div');
  71.             tooltip_div.innerHTML = ('<table class="tooltip_table"><tr><td class="tooltip_head">'+title+'</td></tr><tr><td class="tooltip_content">'+content+'</td></tr></table>');
  72.             realign_tooltip();
  73.             tooltip_div.style.visibility = 'visible';
  74.         }
  75.         function hide_tooltip()
  76.         {
  77.             tooltip_div.style.visibility = 'hidden';
  78.             tooltip_div = null;
  79.         }
  80.         function realign_tooltip(ev)
  81.         {
  82.             ev = (ev || window.event);
  83.             init_tooltip();
  84.             if(tooltip_div)
  85.             {
  86.                 if(document.all)
  87.                 {
  88.                     tooltip_div.style.left = ((((ev.clientX+tooltip_x_offset+tooltip_div.clientWidth) <= body_width) ? (ev.clientX+tooltip_x_offset) : (body_width-tooltip_div.clientWidth))+'px');
  89.                     tooltip_div.style.top = ((((ev.clientY+tooltip_y_offset+tooltip_div.clientHeight) <= body_height) ? (ev.clientY+tooltip_y_offset) : (body_height-tooltip_div.clientHeight))+'px');
  90.                 }
  91.                 else
  92.                 {
  93.                     tooltip_div.style.left = ((((ev.pageX+tooltip_x_offset+tooltip_div.clientWidth) <= body_width) ? (ev.pageX+tooltip_x_offset) : (body_width-tooltip_div.clientWidth))+'px');
  94.                     tooltip_div.style.top = ((((ev.pageY+tooltip_y_offset+tooltip_div.clientHeight) <= body_height) ? (ev.pageY+tooltip_y_offset) : (body_height-tooltip_div.clientHeight))+'px');
  95.                 }
  96.                 document.getElementById('debug').innerHTML = (''+body_width+'x'+body_height+'<br>'+tooltip_div.style.left+', '+tooltip_div.style.top);
  97.             }
  98.         }
  99.     </script>
  100. </head>
  101. <body onload="init_tooltip();">
  102.     <a href="#" onmouseover="show_tooltip('Total mutes','This is the total number of automated mutes currently being applied. This number does not include mutes by staff or those who have logged out before an auto-mute expired.');" onmouseout="hide_tooltip();">blah</a>
  103.     <br><br><br><br><a href="#" onmouseover="show_tooltip('lol','More text');" onmouseout="hide_tooltip();">lol</a>
  104.     <br><br><br><br><div id="tooltip_div">&nbsp;</div>
  105.     <div id="debug">0,0</div>

You need to have something link realign_tooltip(event) as a method call instead of realign_tooltip(); You are not passing any event to the function for Firefox to get data from.
Feb 26 '07 #2
Hi Mobius,

These are some things that I've wanted to say in response to a lot of different people's questions.

When you get an error like this, start using 'alerts' and look at the values of certain variables. realign_tooltip expects ev to be an event object, but adding "alert(ev);" as the first line in realign_tooltip shows that ev is not an event object - it is 'undefined'. So we look to see from where the function is called 'from' and we see that it is called from show_tooltip and no arguments are passed. show_tooltip is the function which we call the event handler (or 'listener') because it is called directly by the system when the event occurs. So show_tooltip needs to pass the event object to realign_tooltip. How does show_tooltip receive the event object? In IE (DOM0) you use 'window.event'. In DOM2 browsers the event object is passed as an argument to the event listener.

There are many general patterns and 'best practices' we can use when developing our applications. I will discuss a few of them in the following.

1. No Javascript in HTML.

For example, we want this:
Expand|Select|Wrap|Line Numbers
  1. <a href="#" onmouseover="show_tooltip('lol','More text');" onmouseout="hide_tooltip();">
  2.  
to look more like this:
Expand|Select|Wrap|Line Numbers
  1. <a class="tt1" href="#">text</a>
  2.  
of course we might like to put tooltips on 'any' element:
Expand|Select|Wrap|Line Numbers
  1. <span class="tt2">text</span>
  2.  

2. No HTML in Javascript.

For example, the HTML in this string:
Expand|Select|Wrap|Line Numbers
  1. '<table class="tooltip_table"><tr><td class="tooltip_head">'+title+'</td></tr><tr><td class="tooltip_content">'+content+'</td></tr></table>';
  2.  
should be in the HTML:
Expand|Select|Wrap|Line Numbers
  1. <div id="tooltip_div">
  2. <table id="tooltip_table">
  3. <tr><td id="tooltip_title"></td></tr>
  4. <tr><td id="tooltip_content"></td></tr>
  5. </table>
  6. </div>
  7.  
I would not use a table but that's a personal choice.


3. Register event listeners in script instead of HTML attributes.

Elements are not available to script (as we say 'in the DOM') until after the 'onload' event. So let's register an onload listener and do all our initialization there (much more could be said about 'onload' techniques).
Expand|Select|Wrap|Line Numbers
  1. window.onload = winOnLoad;
  2.  
  3. function winOnLoad()
  4. {
  5.   initTooltips();
  6.   // initialize other things needed by the app
  7. }
  8.  
  9. function initTooltips()
  10. {
  11. }
  12.  

3. Using ID and CLASS for script access to elements.

Using ID and CLASS are not the only ways to access elements and this suggestion is more specific to the tooltip app.

Notice in the above HTML how I've used ID and CLASS attributes. I used ID where there could be only one of that type of object, and I used CLASS where there could be more than one.

I'm using class 'tt1' to associate any element with a specific tooltip. Class 'tt2' is a different tooltip, etc.

Let's store the tooltip titles and text in arrays:
Expand|Select|Wrap|Line Numbers
  1. var tt_title = [
  2. "title 1",
  3. "title 2",
  4. "title 3"
  5. ];
  6.  
  7. var tt_content = [
  8. "text 1",
  9. "text 2",
  10. "text 3"
  11. ];
  12.  
Now let's finish the initialization function. We want to find all elements that have classes 'tt1', 'tt2', 'tt3', etc. and for each element we will register mouseover and mouseout listeners. Also, for each element we will add a new property 'tooltipIndex' which is an index into the tt_title and tt_content arrays. Note that there could be any number of elements with class 'tt1' - and likewise for 'tt2' etc.

I'm going to use my function xGetElementsByClassName but you can use anyone's version of this. As long as it returns an array of elements, all of which have a certain className.
Expand|Select|Wrap|Line Numbers
  1. window.onload = winOnLoad;
  2.  
  3. function winOnLoad()
  4. {
  5.   initTooltips();
  6.   // initialize other things needed by the app
  7. }
  8.  
  9. function initTooltips()
  10. {
  11.   var a, i, tti = 1;
  12.   a = xGetElementsByClassName('tt' + tti);
  13.   while (a.length) {
  14.     for (i = 0; i < a.length; ++i) {
  15.       a[i].tooltipIndex = tti;
  16.       a[i].onmouseover = ttOnMouseover;
  17.       a[i].onmouseout = ttOnMouseout;
  18.     }
  19.     ++tti;
  20.     a = xGetElementsByClassName('tt' + tti);
  21.   }
  22. }
  23.  
  24. function ttOnMouseover(e)
  25. {
  26.   // "this" points to the element moused over
  27.   var e = document.getElementById('tooltip_title');
  28.   if (e) e.innerHTML = tt_title[this.tooltipIndex];
  29.   e = document.getElementById('tooltip_content');
  30.   if (e) e.innerHTML = tt_content[this.tooltipIndex];
  31.   // now position and show 'tooltip_div' 
  32.   // here you could register a mousemove listener
  33. }
  34.  
  35. function ttOnMouseout(e)
  36. {
  37.   // hide 'tooltip_div'
  38.   // remove mousemove listener
  39. }
  40.  
Feb 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Danny | last post by:
How can I get the coordinates of the mouse cursor in Mozilla browsers as well as Opera and IE6? I'm struggling to understand how to capture mouse movement events with Mozilla/Netscape/Firefox and...
1
by: Jay | last post by:
I need to be able to move a Panel on a windows Form at run time using the mouse. I have tried adding a MouseDown event handler to the Panel and then within the MouseDown handler adding a MouseMove...
3
by: Logan Mckinley | last post by:
I need to be able to detect mouse movement even when it is not over my application. I can get the mouse cords using MousePosition but I am not sure if there is an event that hits my program when...
1
by: Sakharam Phapale | last post by:
Hi All, How to capture Mouse Single click and mouse double click event on Commnad Button. I am doing as follows. Private void Button1_MouseUp(Object sender,...
4
by: Henry Wu | last post by:
Hi, I see examples of Magnifying an area under mouse coordinates to a form or picturebox using VB6 and VC6, does anyone know how to do it under VB.NET? Its nice to use the new GDI+ for it. ...
3
by: Dave | last post by:
Hi, I have a control on my vb app form that dont cath a mouse event`s how can i catch a mouse event on that control and pass it to a function in my main form??? In VB-6 i used the setcapture api...
2
by: bretth | last post by:
In a VB.Net Windows Forms application, I have a user control that handles mouse events. Another section of code programmatically adds a label to the control. I would like label to ignore all...
4
by: Venkatesh | last post by:
Hello All, I have an iframe in my main html and within iframe, I'm loading another HTML webpage. In my main html page, I've captured the mouse click event, by setting the "onclick" for <bodyof...
4
by: mbatestblrock | last post by:
I hope this makes some sense. My ultimate goal here is to execute a block of code if the mouse has not moved in a minute or so within the broswer. The machine I am running this on is for internal...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.