<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Cynic Gazette &#187; javascript</title>
	<atom:link href="http://www.cynicgazette.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cynicgazette.com</link>
	<description>Web Design, Skepticism, Photography and Social Comment</description>
	<lastBuildDate>Thu, 08 Oct 2009 18:31:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Easy multi-line tooltips with Javascript</title>
		<link>http://www.cynicgazette.com/2009/05/06/easy-multi-line-tooltips-with-javascript/</link>
		<comments>http://www.cynicgazette.com/2009/05/06/easy-multi-line-tooltips-with-javascript/#comments</comments>
		<pubDate>Wed, 06 May 2009 16:01:59 +0000</pubDate>
		<dc:creator>Joisey Mike</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[non-intrusive]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.cynicgazette.com/?p=171</guid>
		<description><![CDATA[Tooltips are helpful in a semantic web world where the use of the &#8220;alt&#8221; attribute is restricted to those times an image won&#8217;t show because you&#8217;re  a jerk who fat fingered the image URL.
Lets get right to it since I&#8217;m busy as hell today.
The tooltip is a shorthanded way of referring to the title attribute of [...]]]></description>
			<content:encoded><![CDATA[<p>Tooltips are helpful in a semantic web world where the use of the &#8220;alt&#8221; attribute is restricted to those times an image won&#8217;t show because you&#8217;re  a jerk who fat fingered the image URL.</p>
<p>Lets get right to it since I&#8217;m busy as hell today.</p>
<p>The tooltip is a shorthanded way of referring to the <strong>title</strong> attribute of a link or image (most commonly) though it can be used on other HTML elements. Since we&#8217;re all SEO experts around here, we know the only way to fly is to make a link like this:</p>
<pre>&lt;a href="link-url.html" title="Descriptive and relevant keyword phrase"&gt;revevant text for destination&lt;/a&gt;</pre>
<p>But what of the cold, huddled masses who shout from the mountain tops &#8220;But Cynic, I want a two-line tooltip!&#8221; Well, sure. We can do that with Javascript.</p>
<ol>
<li>Parse out and replace text in static tooltip.</li>
<li>Write the tooltip with a function</li>
</ol>
<h2>Parse out and replace text in static tooltip</h2>
<p>Lets toss a bit of text in our title attribute to tell us where we want our line break. I&#8217;m going to pretend I&#8217;m on a forum and make a quicktag-looking [br]:</p>
<pre>&lt;a href="link-url.html" title="Descriptive and[br]relevant keyword phrase"&gt;hi mom&lt;/a&gt;</pre>
<p>Now we need to create that linefeed. Lets make a function, and pop it into the head tag (or linked .js file).</p>
<pre>function makeMyLinefeedReal(imageElement) {
  var titleAttribute;
  titleAttribute = imageElement.title;
  titleAttribute = titleAttribute.replace(/\[br\]/, String.fromCharCode(10));
  imageElement.title = titleAttribute;
}</pre>
<p><span>And finally call it from the element.</span></p>
<p><span style="font-family: 'Courier New'; line-height: 18px; white-space: pre;">&lt;a href=&#8221;link-url.html&#8221; title=&#8221;Descriptive and[br]relevant keyword phrase&#8221; onmouseover=&#8221;makeMyLinefeedReal(this)&gt;hi mom&lt;/a&gt;</span></p>
<p>Done and done. If you want to get really fancypantsed, you can call this from the BODY&#8217;s onload, or document.ready if you&#8217;re awesomesauce enough to use jQuery.</p>
<h2>Write the tooltip with a function</h2>
<p>Keep in mind a web crawler isn&#8217;t goin go to see something created with javascript, so sure this next method sparingly&#8211;we&#8217;re going to write the whole title attribute with Javascript. You get to feel good about having neater HTML, and you can toss &#8220;non-intrusive clientside DOM scripting&#8221; on your resume.</p>
<p>I&#8217;m currently using this for tooltips on a page of pie charts right now for a full-time-jobby-job project which won&#8217;t be indexed, and even if it was available to the public, I&#8217;m not worried about SEO for the statictical breakdowns on the pie charts; just of the page containing the statistics. (Btw&#8211;coming soon: Kickass tutorial on making webtastic pie charts using Adobe Photoshop and Illustrator)</p>
<p>So here&#8217;s our tag, with a normal attribute.</p>
<pre>&lt;img src="link-url.png" id="favoritePies" height="100" width="100" alt="Pie chart of my favorite pies" /&gt;</pre>
<p>Our function can now take advantage of an escaped new line, <strong>\n</strong>, instead of having to mess with <strong>String.fromCharCode(10)</strong>.</p>
<pre><span style="font-family: Georgia; line-height: 19px; white-space: normal; ">f</span>unction makeMyLinefeed(imageElement) {</pre>
<pre>  imageElement.title = "This is my title\nAnd this is line 2!";</pre>
<pre>}</pre>
<p>And&#8230;</p>
<pre>&lt;img src="link-url.png" id="favoritePies" height="100" width="100" alt="Pie chart of my favorite pies" onmouseover="makeMyLinefeed(this)"/&gt;</pre>
<p>Easy, right? I&#8217;ll let you get trickier than a straigh line of text on your own. One more version for you: here&#8217;s the non-intrusive javascript version. Start with your tag:</p>
<pre>&lt;img src="link-url.png" id="favoritePies" height="100" width="100" alt="Pie chart of my favorite pies" /&gt;</pre>
<p>And this next line does all the fun stuff without any <strong>onmouseover</strong> attribute:</p>
<pre>document.getElementById("favoritePies").mouseover = function () { this.title = "This is my title\nAnd this is line 2!"; }</pre>
<p>So there we go, you have a few different ways to toss a line break into a title attribute. Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cynicgazette.com/2009/05/06/easy-multi-line-tooltips-with-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
