Javascript Content Pane

I'm fond of any trick that can replace javascript functionality in a web page with pure CSS/HTML.  Probably the best example of a CSS javascript replacement is the Suckerfish Dropdown menu at HTMLDog, which I routinely use in my sites.  Anyway, I was poking around today for a way to prevent a javascript content switcher from jumping around based on the hash tag, and I found this sweet CSS content switcher.  It works by setting a fixed size wrapper element (ul or div), then setting the nested elements to match the size.  When you target the id of the display element with any link on the page, the content shifts inside the frame to whichever element you're looking for. 

Unfortunately, the project I'm working on now can't use a fixed-size div, since the content is dynamically generated, but the concept is pretty sweet regardless.  It's definitely something to add to my bag of tricks. Incidentally, if you are looking for a way to keep the hash anchor in the URL while using tab-style javascript navigation in a content switcher, I found that if I set the hash name at window.location.hash manually, then added a space, it would allow you to keep the current "tab" value in the URL but prevent the big jump when the browser tries to follow an anchor. 

<p>function showMe(li) {<br> var divsToHide = document.getElements("div.widget_display");<br> var divNameToShow ="_tab");<br> <br> if(document.getElementById(divNameToShow[0])) {<br>&nbsp;&nbsp;&nbsp;&nbsp; document.getElements("li.current").removeClass("current");<br>&nbsp;&nbsp;&nbsp;&nbsp; li.addClass("current");<br> for(var i=0;i&lt;divsToHide.length;i++) {<br>&nbsp;&nbsp;&nbsp;&nbsp; //Mootools function<br>&nbsp;&nbsp;&nbsp;&nbsp; divsToHide[i].addClass("hidden");<br> }<br> document.getElementById(divNameToShow[0]).removeClass("hidden");<br> } else {<br>&nbsp;&nbsp;&nbsp;&nbsp; //We should never hit this point, but as a last resort, we should notify the user if we are <br>&nbsp;&nbsp;&nbsp;&nbsp; //looking for something that doesn't exist.<br>&nbsp;&nbsp;&nbsp;&nbsp; alert("Sorry, that information doesn't exist for this listing!");<br> }<br> //Set the hash to the div name plus a space.&nbsp; This prevents us from jumping to the hash point <br> //(since that anchor technically doesn't exist), but also allows us to use the reference<br> //if we are linking to this page.&nbsp; The space will be removed in init() when we load the page.<br> window.location.hash = divNameToShow[0]+ ' ';<br> //No need to return false. &nbsp;<br>}</p>


In our onload script, you have to remove the space and use the hash string to set the current tab.  My solution:

<p>function init() {<br> <br>&nbsp;&nbsp;&nbsp;&nbsp; var links = document.getElements("li.widget_tab");</p>
<p>&nbsp;&nbsp;&nbsp; for(var i=0;i&lt;links.length;i++) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; links[i].onclick = getClickSource;<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; var divs = document.getElements("div.widget_display").addClass("hidden");<br><br>&nbsp;&nbsp;&nbsp; divs[0].removeClass("hidden");<br>&nbsp;&nbsp;&nbsp;&nbsp; var divTabHeadings = document.getElements("div.widget_display h2").addClass("hidden");<br>&nbsp;&nbsp;&nbsp; //If there is a hashed location set already, let's jump to it. &nbsp;<br>&nbsp;&nbsp;&nbsp; if(window.location.hash){<br>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; hash=window.location.hash.replace(" ", "");&nbsp; //Take out the space we added<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getTabAndShow(hash);<br>&nbsp;&nbsp;&nbsp; }<br>}</p>


I'm not sure how well this script is going to work across the browser spectrum, but I have confirmed that it does work in Firefox 3.5+, IE7, and IE8.  Chrome is apparently smart enough to remove our space and jump to the ID.  We could make it something like a period to overcome this, but I'd rather have a jumpy browser than illegal characters in my URL.