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.
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