Switch content (unobtrusive js)

I've realised that a lot of the javascript on this site is kind of out-of-date. The trend these days is towards 'unobtrusive' javascript, which basically means that you don't add anything extra to the HTML of the page - just like with CSS. I think this is a good trend and I hope to do all (or most) javascript this way in future.

Explanation : Part 1

When the page loads, the hidestories() function is called (with the 'window.onload' bit). This finds the #stories div then hides all the divs inside that with the class 'story' and makes them 'display:none;'. Then, to show the first story onload, a variable is sent to the stories() function.

The stories() function assigns another function (with no name) to the onclick event of each of the links at the top. It finds the element with the id #thebuttons and identifies the links in that. The function that is attached to the links first runs the hidestories() function, then takes the part of the link after the '#' and turns it into a variable. The div that was being linked to is then displayed ('display:block;') and the link itself returns false.

Explanation : Part 2

The good thing about this is that if javascript is disabled, the links will work as normal and all the content will still be readable.

The bad thing about this - just like on (the page I had here before) - is that when the page first loads, you can see all of the content for a second before the javascript kicks in and hides it. If you're on an intranet and are able to control the browsers, or if you just don't care about those without javascript, you can fix this by setting the class 'story' to 'display:none;' in your CSS.

Explanation : Part 3

Another small point is that I haven't added anything here that highlights the current content in the menu at the top. It's not too difficult to do, I just wanted to keep this as simple as possible. Here's an example.

Of course, you can also do this whole 'switch content' thing with AJAX. My demo uses AJAX (basically javascript, php, and the xml 'httprequest') to change the content of the page by only refreshing the content part. This also doesn't work if javascript is disabled, so my demo uses PHP to include the content into a template page if the javascript fails.

HTML
<h1>Switch content (unobtrusive js)</h1>
<p>I've realised that a lot of the javascript on this site is kind of
out-of-date. The trend these days is towards 'unobtrusive' javascript, which basically means that you don't add anything extra to the HTML of
the page - just like with CSS. I think this is a good trend and I hope
to do all (or most) javascript this way in future.</p>
<ul id="thebuttons">
<li><a href="#storyA">Story A</a></li>
<li><a href="#storyB">Story B</a></li>
<li><a href="#storyC">Story C</a></li>
</ul>
<div id="stories">
<div id="storyA" class="story">
<h2>Explanation : Part 1</h2>
<p>When
the page loads, the hidestories() function is called (with the
'window.onload' bit). This finds the #stories div then hides all the
divs inside that with the class 'story' and makes them 'display:none;'.
Then, to show the first story onload, a variable is sent to the stories() function.</p>
<p>The
stories() function assigns another function
(with no name) to the onclick event of each of the links at the top. It
finds the element with the id #thebuttons and identifies the links in
that. The function that is attached to the links first runs the
hidestories() function, then takes the part of the link after the '#' and turns it into a variable. The div that was being linked
to is then displayed ('display:block;') and the link itself returns
false.</p>
</div>
<div id="storyB" class="story">
<h2>Explanation : Part 2</h2>
<p>The
good thing about this is that if javascript is disabled, the links will
work as normal and all the content will still be readable.</p>
<p>The bad thing about this - just like on (<a href="http://bonrouge.com/br.php?page=switchcontent">the page I had here before</a>)
- is that when the page first loads, you can see all of the content for
a second before the javascript kicks in and hides it. If you're on an
intranet and are able to control the browsers, or if you just don't
care about those without javascript, you can fix this by setting the
class 'story' to 'display:none;' in your CSS.</p>
</div>
<div id="storyC" class="story">
<h2>Explanation : Part 3</h2>
<p>Another
small point is that I haven't added anything here that highlights the
current content in the menu at the top. It's not too difficult to do, I
just wanted to keep this as simple as possible. <a href="br.php?page=switchcontent4">Here's an example</a>.</p>
<p>Of course, you can also do this whole 'switch content' thing <a href="http://bonrouge.com/br.php?page=ajaxswitch">with&nbsp;AJAX</a>.
My demo uses AJAX (basically javascript, php, and the xml 'httprequest')
to change the content of the page by only refreshing the content part.
This also doesn't work if javascript is disabled, so my demo uses PHP
to include the content into a template page if the javascript fails.</p>
</div>
</div>
CSS
#thebuttons {
list-style:none;
width:24em;
margin:auto;
}
#thebuttons li {
float:left;
width:8em;
font-weight:bold;
background-color:yellow;
padding:0.5em 0;
margin:1em auto;
text-align:center;
}
#stories {
width:30em;
margin:1em auto;
border:1px solid purple;
clear:left;
padding:1em;
}
Javascript
function hidestories() {
var divs=document.getElementById('stories').getElementsByTagName('div');
for (j=0; j<divs.length; j++) {
var rE = new RegExp("(^|\\s)" + 'story' + "(\\s|$)");
if (rE.test(divs[j].className)) {
divs[j].style.display="none";
}
}
}

function stories(first) {
var thebuttons=document.getElementById('thebuttons').getElementsByTagName('a');
for (i=0; i<thebuttons.length; i++) {
thebuttons[i].onclick=function() {
hidestories();
var thestory=(this.href).split("#",2)[1];
document.getElementById(thestory).style.display="block";
return false;
}
}
if (first) {
var firstone=document.getElementById('stories').firstChild;
if (firstone.nodeType != 1) {firstone = firstone.nextSibling;}
firstone.style.display="block";
}
}
window.onload=function() {
hidestories();
stories(1);
}

Comments

#1
2006-10-16 Louis says :

Just used this script as a much needed solution on a website I'm designing. Quick note to say thanks for the effort you put into writing it.

Cheers

#2
2006-12-17 Todd says :

What if I wanted to use this for videos set to autoplay? Each time I'd click a new video, if the current one is still playing, it will continue to do so in the background, behind the newer video. Any remedy for this?

Love the script by the way. Thanks so much.

#3
2007-02-05 Sean says :

I spent 6 hours searching for a script similar to this....thank you so much for posting and sharing......

#4
2007-09-11 brad says :

loads of useful stuff here, thanks for taking the time to make it available.

Brad

#5
2008-03-17 Josh says :

I'm using this concept in my page, but im trying to use the <li><a href="#storyA">Story A</a></li> part inside a "story" but it wont change to the new content, any suggestions please email me at x_anubis@msn.com

#6
2008-06-30 Jen says :

Can I use this script and have other pages link to it? I am thinking of using this code as my portfolio page. Listing all the logos of companies i done in storyA then a description of each of those companies in other stories. But can i link to a specific story on other parts of my page... like portfolio.html#storyC or something like that?

This site rocks! Thank you for all your help!

#7
2008-06-30 Jen says :

This is an awesome script! Thank you for sharing!

I am wanting to use it on a portfolio page, I have it working on the list of companies on the side. But I have storyA with all the logos of the companies, I would like to make the choice of clicking on the tex link on the side (your script with <ulid="thebuttons">
<li><a href="#storyA">Story A</a></li> ) OR give the option of clicking on the logo in storyA. That would then display the story of their choice either way.
How in the world do i do that?! I cant seem to figure it out!

Thanks so much for all your scirpts! They are awesome!
Jen

#8
2008-06-30 BonRouge says :

Jen,
I've played around with it a little. See if this helps.

#9
2008-06-30 Jen says :

Bon,
Thank you so much for helping me out! I have been pulling my hair out all day!
It does indeed show the storyB, but it doesnt get rid of storyA.
Maybe the probelm is that the link to storyB IS IN storyA. Because storyA contains logos and on click takes them to other stories about that specific company.)
It sortof works. just didnt hide storyA.

Any ideas?
Jen

#10
2008-06-30 Jen says :

I TAKE IT ALL BACK!!!!
It worked!!! WHOOO-HOOOO!!!
You are a genious!
I just forgot to change the stories to tstories!

YOU ROCK!!! THANK YOU SOOOO MUCH!!!!
Jen

#11
2008-07-01 BonRouge says :

Jen,
Glad it worked for you. smile

Comment form

Please type the word 'sexy' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url