Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Tooltip box

Reply
Thread Tools

Tooltip box

 
 
Kim
Guest
Posts: n/a
 
      11-04-2008
Using the code below am I able to display/hide a tooltip without any
problems, however once the tooltip is displayed its position is fixed
(based on where the mouse first hovered onto the object) and I would
like the tooltip to follow the mouse instead.
What must I change to do this ?


HTML
<a href="#" onMouseOver="showBox('help','text to display')"
onMouseOut="hideBox('help')">text link</a>

JS
function hideLayer(strLayer) {
if (document.getElementById) {
d = document.getElementById(strLayer);
if (d) {
d.style.visibility = 'hidden';
d.style.display = 'none';
}
}
else if (document.all) {
d = eval('document.all[\''+strLayer+'\']');
if (d) {
eval('document.all[\''+strLayer+'\'].style.visibility = \'hidden
\'');
eval('document.all[\''+strLayer+'\'].style.display = \'none\'');
}
}
}
function showLayer(strLayer) {
if (document.getElementById) {
d = document.getElementById(strLayer);
d.style.visibility = 'visible';
d.style.display = 'block';
}
else if (document.all) {
eval('document.all[\''+strLayer+'\'].style.visibility = \'visible
\'');
eval('document.all[\''+strLayer+'\'].style.display = \'block\'');
}
}
function getObj(name) {
if (document.getElementById) {
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all) {
this.obj = document.all[name];
this.style = document.all[name].style;
}
else if (document.layers) {
this.obj = document.layers[name];
this.style = document.layers[name];
}
return this;
}
document.onmousemove = getMousePosition;
if (!document.all) document.captureEvents(Event.MOUSEMOVE);
var mouse_x = 0;
var mouse_y = 0;
function getMousePosition(e) {
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
mouse_x = e.pageX;
mouse_y = e.pageY;
}
else if (e.clientX || e.clientY) {
mouse_x = e.clientX + (document.documentElement ?
document.documentElement.scrollLeft : document.body.scrollLeft);
mouse_y = e.clientY + (document.documentElement ?
document.documentElement.scrollTop : document.body.scrollTop);
}
}
function showBox(name, msg) {
var x = (mouse_x + 20) + 'px';
var y = (mouse_y + 0) + 'px';
var box = getObj(name);

box.obj.innerHTML = msg;
box.style.position = 'absolute';
box.style.top = y;
box.style.left = x;
showLayer(name);
}
function hideBox(name) {
hideLayer(name);
}

 
Reply With Quote
 
 
 
 
Robin
Guest
Posts: n/a
 
      11-04-2008
Kim wrote:
> Using the code below am I able to display/hide a tooltip without any
> problems, however once the tooltip is displayed its position is fixed
> (based on where the mouse first hovered onto the object) and I would
> like the tooltip to follow the mouse instead.
> What must I change to do this ?


There are many libraries to do this so why reinvent the wheel? Anyway,
the simplest change I can see:

> var mouse_x = 0;
> var mouse_y = 0;


var box = null; // moved to be a global

> function getMousePosition(e) {
> if (!e) var e = window.event;
> if (e.pageX || e.pageY) {
> mouse_x = e.pageX;
> mouse_y = e.pageY;
> }
> else if (e.clientX || e.clientY) {
> mouse_x = e.clientX + (document.documentElement ?
> document.documentElement.scrollLeft : document.body.scrollLeft);
> mouse_y = e.clientY + (document.documentElement ?
> document.documentElement.scrollTop : document.body.scrollTop);
> }


if (box) placeBox();

> }
> function showBox(name, msg) {

box = getObj(name); // note no 'var' using global
// should really be a check failure here
> box.obj.innerHTML = msg;
> box.style.position = 'absolute';

placeBox();
> showLayer(name);
> }
>
> function hideBox(name) {
> hideLayer(name);

box = null;
> }
>


function placeBox() {
box.style.top = (mouse_x + 20) + 'px';
box.style.left = (mouse_y + 0) + 'px';
}

Robin
 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      11-04-2008
On Nov 4, 9:32*am, Kim <kims...@gmail.com> wrote:
> Using the code below am I able to display/hide a tooltip without any
> problems, however once the tooltip is displayed its position is fixed


Without any problems that you can see.

> (based on where the mouse first hovered onto the object) and I would
> like the tooltip to follow the mouse instead.


That is so irritating and pointless.

> What must I change to do this ?
>
> HTML
> <a href="#" onMouseOver="showBox('help','text to display')"
> onMouseOut="hideBox('help')">text link</a>


Link goes nowhere? Might navigate to the top of the page in some
browsers.

>
> JS
> function hideLayer(strLayer) {
> * * * * if (document.getElementById) {


Do not test host object methods in this manner. Use the typeof
operator. Search the group for "isHostMethod".

> * * * * * * * * d = document.getElementById(strLayer);
> * * * * * * * * if (d) {
> * * * * * * * * * * * * d.style.visibility = 'hidden';
> * * * * * * * * * * * * d.style.display = 'none';
> * * * * * * * * }
> * * * * }
> * * * * else if (document.all) {


Here's where the wheels come off.

> * * * * * * * * d = eval('document.all[\''+strLayer+'\']');
> * * * * * * * * if (d) {
> * * * * * * * * * * * * eval('document.all[\''+strLayer+'\'].style.visibility = \'hidden
> \'');
> * * * * * * * * * * * * eval('document.all[\''+strLayer+'\'].style.display = \'none\'');
> * * * * * * * * }
> * * * * }}


Where did you pick that up? If you find yourself using eval, you are
likely off in the weeds.

>
> function showLayer(strLayer) {
> * * * * if (document.getElementById) {
> * * * * * * * * d = document.getElementById(strLayer);
> * * * * * * * * d.style.visibility = 'visible';
> * * * * * * * * d.style.display = 'block';
> * * * * }
> * * * * else if (document.all) {
> * * * * * * * * eval('document.all[\''+strLayer+'\'].style.visibility = \'visible
> \'');
> * * * * * * * * eval('document.all[\''+strLayer+'\'].style.display = \'block\'');
> * * * * }}


Same comments for this virtual duplication.

>
> function getObj(name) {
> * * * * if (document.getElementById) {
> * * * * * * * * this.obj = document.getElementById(name);
> * * * * * * * * this.style = document.getElementById(name).style;
> * * * * }
> * * * * else if (document.all) {
> * * * * * * * * this.obj = document.all[name];
> * * * * * * * * this.style = document.all[name].style;
> * * * * }


No eval this time?

> * * * * else if (document.layers) {
> * * * * * * * * this.obj = document.layers[name];
> * * * * * * * * this.style = document.layers[name];
> * * * * }


Clearly you can lose this branch. It is only for NN4 and makes no
sense in the context of this script.

> * * * * return this;}
>
> document.onmousemove = getMousePosition;


It is good practice to declare functions before their first use.

> if (!document.all) document.captureEvents(Event.MOUSEMOVE);


That might be the worst object inference (a form of browser sniffing)
I have ever seen. Where did you find this stuff?

> var mouse_x = 0;
> var mouse_y = 0;
> function getMousePosition(e) {
> * * * * if (!e) var e = window.event;


e = e || window.event;

> * * * * if (e.pageX || e.pageY) {


Wrong. What if both are 0?

> * * * * * * * * mouse_x = e.pageX;
> * * * * * * * * mouse_y = e.pageY;
> * * * * }
> * * * * else if (e.clientX || e.clientY) {


Same here.

> * * * * * * * * mouse_x = e.clientX + (document.documentElement ?
> document.documentElement.scrollLeft : document.body.scrollLeft);


Wrong. The documentElement property is defined in quirks mode. You
should be testing the scrollLeft/Top properties.

> * * * * * * * * mouse_y = e.clientY + (document.documentElement ?
> document.documentElement.scrollTop : document.body.scrollTop);
> * * * * }}


Same here.

>
> function showBox(name, msg) {
> * * * * var x = (mouse_x + 20) + 'px';
> * * * * var y = (mouse_y + 0) + 'px';
> * * * * var box = getObj(name);
>
> * * * * box.obj.innerHTML = msg;
> * * * * box.style.position = 'absolute';
> * * * * box.style.top = y;
> * * * * box.style.left = x;
> * * * * showLayer(name);}
>
> function hideBox(name) {
> * * * * hideLayer(name);
>
> }
>
>


I advise you to look into the title attribute. Most browsers will
turn it into a tooltip. Furthermore, search engines, screen readers,
text browsers, etc. will be able to see the content. No script
required. That should save you a lot of trouble.
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-04-2008
On Nov 4, 10:01*am, Robin <a...@somewhere.com> wrote:
> Kim wrote:
> > Using the code below am I able to display/hide a tooltip without any
> > problems, however once the tooltip is displayed its position is fixed
> > (based on where the mouse first hovered onto the object) and I would
> > like the tooltip to follow the mouse instead.
> > What must I change to do this ?

>
> There are many libraries to do this so why reinvent the wheel? Anyway,
> the simplest change I can see:


Name one that is worthwhile. Wheels should be re-invented when
previous attempts fall short.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-04-2008
David Mark wrote:
> Kim wrote:
>> return this;}
>>
>> document.onmousemove = getMousePosition;

>
> It is good practice to declare functions before their first use.
>
> [...]
>> function getMousePosition(e) {


If so, that practice is at least not supported by language rules: All
declarations are instantiated *before* execution, so order does not matter.

I found that with languages like this some people declare their functions on
the bottom, presumably so that the main code can be edited easier. I
haven't done that yet, but I see no good reason why I shouldn't. Can you
name one?


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-04-2008
On Nov 4, 5:03*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> David Mark wrote:
> > Kim wrote:
> >> * * * * return this;}

>
> >> document.onmousemove = getMousePosition;

>
> > It is good practice to declare functions before their first use.

>
> > [...]
> >> function getMousePosition(e) {

>
> If so, that practice is at least not supported by language rules: *All
> declarations are instantiated *before* execution, so order does not matter.


No kidding.

>
> I found that with languages like this some people declare their functionson
> the bottom, presumably so that the main code can be edited easier. *I
> haven't done that yet, but I see no good reason why I shouldn't. *Can you
> name one?


Harder to follow for one.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-04-2008
David Mark wrote:
> Thomas 'PointedEars' Lahn wrote:
>> David Mark wrote:
>>> Kim wrote:
>>>> return this;}
>>>> document.onmousemove = getMousePosition;
>>> It is good practice to declare functions before their first use.
>>> [...]
>>>> function getMousePosition(e) {

>> If so, that practice is at least not supported by language rules: All
>> declarations are instantiated *before* execution, so order does not matter.

>
> No kidding.
>
>> I found that with languages like this some people declare their functions on
>> the bottom, presumably so that the main code can be edited easier. I
>> haven't done that yet, but I see no good reason why I shouldn't. Can you
>> name one?

>
> Harder to follow for one.


Why is scrolling down harder to follow than scrolling up?
And with an Outline View the issue gets even smaller. *shrug*


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-05-2008
On Nov 4, 5:30*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

[snip]

>
> Why is scrolling down harder to follow than scrolling up?


Clearly it isn't. But one can scroll up (or search up or whatever)
knowing that the identifier will be defined above, so it eliminates
the step of looking in the other direction.

> And with an Outline View the issue gets even smaller. *shrug*


I never use such things, so I don't know.
 
Reply With Quote
 
rf
Guest
Posts: n/a
 
      11-05-2008

"David Mark" <> wrote in message
news:7a912b25-9457-434f-933c-...
On Nov 4, 5:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

[snip]

>
>> Why is scrolling down harder to follow than scrolling up?


> Clearly it isn't. But one can scroll up (or search up or whatever)
> knowing that the identifier will be defined above, so it eliminates
> the step of looking in the other direction.


And if they are at the bottom one can scroll down (or search down or
whatever) knowing that the identifier will be defined below, so it
eliminates the step of looking in the other direction.

Also, down is the default search direction in my editors, I don't have to
change it.

When opening a file the overall top level logic is at the top, ready to
read. If the (hopefully static and never changed much) subroutines are at
the top instead one has to search for the overall top level logic. A PITA if
it is not inside a function itself.


 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-05-2008
On Nov 4, 8:31*pm, "rf" <r...@invalid.com> wrote:
> "David Mark" <dmark.cins...@gmail.com> wrote in message
>
> news:7a912b25-9457-434f-933c-...
> On Nov 4, 5:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
> wrote:
>
> [snip]
>
>
>
> >> Why is scrolling down harder to follow than scrolling up?

> > Clearly it isn't. *But one can scroll up (or search up or whatever)
> > knowing that the identifier will be defined above, so it eliminates
> > the step of looking in the other direction.

>
> And if they are at the bottom one can scroll down (or search down or


If you put your function declarations after the first use as a rule,
then that should work for you. Seems an odd choice though.

[snip]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How could I add a tooltip to an iframe ? Also, the tooltip contents has to be determine @ run-time ! Radu ASP .Net 1 01-08-2007 04:11 PM
Tooltip on Dropdown (Combo Box) M P ASP General 4 05-02-2006 09:43 AM
tooltip for the options of a drop down list box nagesh Javascript 1 04-23-2005 01:21 AM
Adding tooltip tip tp list box =?Utf-8?B?U2hpanUgUG95aWxpbA==?= ASP .Net 3 07-19-2004 12:26 PM
How to display an individual tooltip for each item in a listbox (select box) hiroshi ochi Javascript 3 05-13-2004 03:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57