SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simply javascript book hide tooltip function question

    Hi all

    have a quick question on the Simply Javascript book.

    On page 139 there is a hideTip function. Which is about hiding a tooltip during a 'mouseout' or blur.

    hideTip: function(link)
    {
    if (link._tooltip)
    {
    link.title = link._tooltip.childNodes[0].nodeValue;
    link.removeChild(link._tooltip);
    link._tooltip = null;

    }
    },


    My question is why is there a need for the first line, where link.title stores the tooltip text (why???). Why can't you just remove link._tooltip...

    Any help/suggestions would be much appreciated. What am i missing (apart from a decent brain)

    Thanks for your time

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to re-apply the link's title because you previously emptied it for "cleanliness"...

    Read the second paragraph on p.138.

    (I just happen to have my copy at hand )
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the prompt response.

    I do have another issue now though...

    link.title = "";

    So the links title property was set to an empty string. Because the tooltip would not be displayed twice.

    My question here is - why would it contain the tooltip twice? Is this a case of one tooltip from the showtip function and another from the default (html) action? Its just that the showtipListener function (the function that calls the showtip function) has a preventDefault method.

    What am I missing (again)
    Last edited by pug2112; Jun 11, 2008 at 06:26. Reason: to make it clearer

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    That preventDefault may not work in all browsers to prevent the showing of the standard tooltip, which is why a good standards-based method was used to remove the title, thus removing the possibility for the title to accidentally be used as a default tooltip.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the information.

    I tried the example again on various browsers.

    Firstly I commented out

    link.title = ""; //from showTip function and
    link.title = link._tooltip.childNodes[0].nodeValue; // from hideTip function.

    So now was relying totally on the preventDefault function called in the showTipListener function. In most browsers the tooltip was displayed twice (both through JavaScript and HTML). This was tested in IE7, Firefox 2, Netscape 8 and Opera 9.

    In all the preventDefault was only 'useful' for Opera 9.

    I then tested it by only commenting out the 'preventDefault' function and the example worked fine (html tooltip not shown) for all browsers.

    I understand I havent tested older browsers and it was a crude test at that. But it seems the example worked well without the need of the preventDefault function. Was just wondering would there be a scenario (older browser) where the preventDefault function would be needed.

    I understand its good programming practice to include the preventDefault function (and would include it). But the mind wonders....

    Thanks once again for the help guys.

    Infact it would be interesting to find out about the browser support for the preventDefault function...
    Last edited by pug2112; Jun 12, 2008 at 00:29. Reason: a lightbulb just went on in my head

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I suspect that preventDefault is not required, but may have been included as a learning tool.

    http://www.quirksmode.org/js/events_early.html
    Returning false to prevent the default action works in all browsers, it’s a basic part of event handling. Modern event handling models have added some new ways of preventing the default:

    * W3C has added the method preventDefault() to the event. If you call it the default action is prevented.
    * Microsoft has added the property returnValue to the event. If you set it to false the default action is prevented.

    But you don’t need to use them, a simple return false also does the job.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •