<?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>Blue Arena &#187; trail</title>
	<atom:link href="http://www.bluearena.com/tag/trail/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bluearena.com</link>
	<description>Web Development &#38; Digital Marketing</description>
	<lastBuildDate>Wed, 16 Nov 2011 20:37:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Javascript: Shortening a breadcrumb using JQuery</title>
		<link>http://www.bluearena.com/2010/01/javascript-shortening-a-breadcrumb-using-jquery/</link>
		<comments>http://www.bluearena.com/2010/01/javascript-shortening-a-breadcrumb-using-jquery/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 12:50:19 +0000</pubDate>
		<dc:creator>Maloric</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[accessible]]></category>
		<category><![CDATA[breadcrumb]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[short]]></category>
		<category><![CDATA[shorten]]></category>
		<category><![CDATA[too long]]></category>
		<category><![CDATA[trail]]></category>

		<guid isPermaLink="false">http://www.bluearena.com/?p=188</guid>
		<description><![CDATA[A lot of sites these days use breadcrumb trails to make navigation easier for their users. This is essentially a list of every parent category/section of the page you&#39;re on, and might look something like this: Home &#62; About us &#62; Meet the team Each of these is typically a hyperlink back to the relevant [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of sites these days use breadcrumb trails to make navigation easier for their users. This is essentially a list of every parent category/section of the page you&#39;re on, and might look something like this:</p>
<p><a href="#">Home</a> &gt; <a href="#">About us</a> &gt; <a href="#">Meet the team</a></p>
<p>Each of these is typically a hyperlink back to the relevant section. Now these are great, but what happens if your breadcrumb isn&#39;t that small? It&#39;s easy to see a situation where you have a bunch of really long titles that cause the breadcrumb to span over two or three lines. This isn&#39;t always what you want, since it can make the breadcrumb look broken. You might just decide to make some of the titles smaller, but with content managed sites and page names stuffed with SEO keywords, this is often easier said than done.</p>
<p><span id="more-188"></span>So I came up with a solution to this problem using <a href="http://jquery.com/">JQuery</a>. It relies on a certain kind of markup; you must use &lt;ul&gt; and &lt;li&gt; tags to build your breadcrumb rather than a continuous string of text. Hopefully you already know how to style a &lt;ul&gt; as a breadcrumb, but if not, the following should help:</p>
<ul>
<li>Use either float:left, display:inline, or display:inline-block on your &lt;li&gt; tags.</li>
<li>If you want to seperate each item with an image, use the following as well:
<ul>
<li>padding-left:24px (or whathever you need) on your &lt;li&gt; tag</li>
<li>background:url(/images/breadcrumb.gif) left no-repeat; on your &lt;li&gt;</li>
</ul>
</li>
</ul>
<p><em>(I realise the background image could be applied instead as a bullet image, but this doesn&#39;t look great in some older browsers, so the above method is a little safer)</em></p>
<p>You should be able to figure out the rest of the styling from there, but if not, drop me a comment and I&#39;ll help you out.</p>
<h2>The Javascript</h2>
<p>Ok, let me explain what the script does and then I&#39;ll post the code. Firstly, the script has two properties: &quot;breadcrumb&quot; and &quot;maxWidth&quot;. The first is a <a href="http://jquery.com/">JQuery</a> selector for the &lt;ul&gt; element. If not supplied, this defaults to &quot;$(&#39;div#breadcrumb &gt; ul&#39;); a &lt;ul&gt; that is the immediate descendant of a div with the ID of &quot;breadcrumb&quot;. The second is the maximum width in pixels the breadcrumb should be, which defaults to the <a href="http://docs.jquery.com/CSS/outerWidth">outerWidth</a> of the &lt;ul&gt;. If it goes over this width, the breadcrumb is shortened until it fits.</p>
<p>Essentially, what the script does is check the combined width of the breadcrumb&#39;s &lt;li&gt; elements. If this total exceeeds the &quot;maxWidth&quot; property, the script will shorten the first non-shortened list item and check again. It continues doing this until the combined width of all of the list items is less than &quot;maxWidth&quot;. When each item in shortened, the text is replaced by &quot;&#8230;&quot; and to keep things nice and accessible, a title tag is added with the original text.</p>
<p>And that&#39;s that. All that&#39;s left is to show you the script and how to call it:</p>
<pre>function shortenBreadcrumb(options){
    /*
        Shortens the breadcrumb to a specified width by removing the text from one list item
        at a time and replacing it with &quot;...&quot; - accepts an options object as an optional
        parameter with two options:
            shortenBreadcrumb({
                breadcrumb : $(&#39;div#breadcrumb &gt; ul&#39;),
                maxWidth : 725
            });

        The first is the selector for the breadcrumb ul, the second is the maximum width you
        want it to be.
    */

    var breadcrumb = $(&#39;div#breadcrumb &gt; ul&#39;);
    var maxWidth = $(&#39;div#breadcrumb&#39;).innerWidth();

    if (options != undefined){
        if (options.breadcrumb != null) { breadcrumb = options.breadcrumb; }
        if (options.maxWidth != null) { maxWidth = options.maxWidth; }
    }

    var levelCount = breadcrumb.find(&#39;li&#39;).size();
    var shortEnough = false;
    var totalWidth;
    while (shortEnough == false) {
        totalWidth = 0;
        breadcrumb.children(&#39;li&#39;).each (function(){
            totalWidth += $(this).outerWidth(true);
        });
        if (totalWidth &gt; maxWidth){
            var li = breadcrumb.children(&#39;li&#39;).not(&#39;.short&#39;).eq(1);
            li.addClass(&#39;short&#39;);
            li.children(&#39;a&#39;).attr(&#39;title&#39;, li.children(&#39;a&#39;).html());
            li.children(&#39;a&#39;).html(&#39;...&#39;);
        }
        else {
            shortEnough = true;
        }
    }
}
</pre>
<p>Include this script in your page and call using:</p>
<pre>$(document).ready(function(){
    shortenBreadcrumb({
        breadcrumb : $(&#39;ul#breadcrumb&#39;),
        maxWidth : 600
    });
});
</pre>
<p>Of course, any of these two options can be left blank, so you can call it like so:</p>
<pre>    shortenBreadcrumb();
</pre>
<p>It goes without saying that this script requires the <a href="http://jquery.com/">JQuery</a> library. If you want to use the script in your site, please do, but a mention of Blue Arena or a comment would be appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bluearena.com/2010/01/javascript-shortening-a-breadcrumb-using-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

