Greetings.
I'm cross referencing another book that's introducing a new or more "appropriate" way of assigning functions to links, div's..etc. I needed an explanation on this example:
This example displays some text when mousing over a link. The text displayed is specifically styled in CSS. The following code references a js file that makes 'event listeners' browser compatible.
The 'title' attribute of the 'anchor' tag has text that the JS code pulls from to display it when moused over.
HTML/CSS Code (main part)
<p><a class="federation"
title="Read more..." href="...">James Tiberius Kirk</a> ....(more text)...<a class="federation"
title="Read more..." href="...">NCC-1701</a> ...
JavaScript Code (assuming <script type="text/javascript" src="
core.js"></script>)
Code:
var Tooltips =
{
init: function()
{
var links = document.getElementsByTagName ("a");
for (var i = 0; i < links.length; i++)
{
var title = links[i].getAttribute("title");
if (title && title.length > 0)
{
Core.addEventListener(
links[i], "mouseover", Tooltips.showTipListener);
Core.addEventListener(
links[i], "focus", Tooltips.showTipListener);
Core.addEventListener(
links[i], "mouseout", Tooltips.hideTipListener);
Core.addEventListner(
links[i], "blur", Tooltips.hideTipListener);
}
}
},
showTip: function(link)
{
Tooltips.hideTip(link);
var tip = document.createElement ("span");
tip.className = "tooltip";
var tipText = document.createTextNode(link.title);
tip.appendChild(tipText);
link.appendChild(tip);
link._tooltip = tip;
link.title = " ";
// Fix for Safari2/Opera9 repaint issue
document.documentElement.style.position = "static";
}
},
hideTip: function(link)
{
if (link._tooltip)
{
link.title= link._tooltip.childNodes[0].nodeValue;
link.removeChild(link._tooltip);
link._tooltip = null;
// Fix for Safari2/Opera9 repaint issue
document.documentElement.style.position = "static";
}
},
showTipListener: function(event)
{
Tooltips.showTip(this);
Core.preventDefault(event);
},
hideTipListener: function(event)
{
Tooltips.hideTip(this);
}
};
Core.start (Tooltips);
My questions are:
1. In the showTipListener: function(event), what does '
this' in Tooltips.showTip(
this) represent? Does it represent just 'any' of the links in the "links" array at the beginning of the code? Or does it represent only the links with the 'title' attribute?.
2. What's the purpose of this line:
link.title = link._tooltip.childNodes[0].nodeValue;
Why would I need to know the value of the node in order to get rid of it? Couldn't they have done away with that line?
3. Isn't the next line ( link._tooltip = null; ) overkill? So this is saying that after I get rid of it I want to make sure it's gone and nothing is left so I set that space to blank?