It is currently Thu May 08, 2025 9:40 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post Post subject: JavaScript Navigation, Sectioning and Consistency
 
Offline
Godlike Poster
Godlike Poster
Years of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membership
User avatar

Joined: Sun Oct 16, 2005 9:42 am
Posts: 8798
Karma: 17

Location: Imagine in your mind a posh country club
Steam Login Name: azcn2503
This section hasn't seen a good injection of programming in a long time.

I'm going to provide the injection.

Basically I am going to demonstrate how to create a navigation scheme that relies on JavaScript. Because it relies on JavaScript, you can guarantee that it will only work on modern browsers and very modern mobile browsers such as Opera Mobile, Safari for iPhone etc. it's not as robust as a PHP navigation scheme, but some people can't afford to host on a PHP web server, yet still want the scalability of a navigation scheme that doesn't require you to update every single .html file every time you add a new section, or change your layout slightly...

Because of the way the navigation scheme is coded, it pretty much forces consistency.

I am going to demonstrate how to create a tabbed website interface that dynamically shows and hides sections, and changes styles of elements on the page depending on where you clicked, but without refreshing the page or ever loading any additional content (nothing via background header requests either aka AJAX).

I am assuming you have a basic knowledge of JavaScript and are proficient in (X)HTML already (ie you know everything).

So let's begin:

You'll always need a grabber piece of code, something that picks up the elements in your page so that you can change them, based on their ID attribute.

Code:
// Grabber function
function grab(o){
   return document.all?document.all[o]:document.getElementById?document.getElementById(o):"";
}


You will need code that can switch between sections. I'm going to use two functions for this. We need one function to convert section ID text and one function to display and hide the sections, and change the section links (tabs in this case):

Code:
// Section text parse function
function parseSectionName(s){
   return s.replace(/\ /g,"-").toLowerCase(); // Yes this is a regular expression
}
// Section hiding and displaying function
function displaySection(n){
   for(var i=0;i<sections.length;i++){
      var sectionLink=grab("section-link_"+i);
      var sectionDiv=grab("section_"+parseSectionName(sections[i]));
      sectionLink.className=n==i?"section-link_active":"section-link"; // alters appearance of the section links (tabs)
      sectionDiv.className=n==i?"section_active":"section"; // alters appearance of the sections (displays or hides them)
   }
}


You'll also need to define your sections and tabs in an array. The sections and tabs can share the same array, it's no problem.

Code:
// Sections and tabs
var sections=[
   "Home",
   "Products",
   "Company Background",
   "Contact Information"
];


You can quite easily display these tabs using (CSS formatted) links like so:

Code:
<div id="tabs"><script type="text/javascript"><!--
   for(var i=0;i<sections.length;i++){
      document.write("<a href=\"javascript:displaySection("+i+");\" id=\"section-link_"+i+"\" class=\"section-link\">"+sections[i]+"</a>");
   }
//--></script></div>


Now you need to manually add your sections like so:

Code:
<div id="section_home" class="section">Home</div>
<div id="section_products" class="section">Products</div>
<div id="section_company-background" class="section">Company Background</div>
<div id="section_contact-information" class="section">Contact Information</div>


The important bits of CSS you'll need are:

Code:
#tabs a{
   padding-left:10px; padding-right:10px;
}
/* feel free to add borders, background colours etc. to the section-link classes below */
#tabs a.section-link{
   font-weight:normal;
}
#tabs a.section-link_active{
   font-weight:bold;
}
/* feel free to add borders, background colours etc. to the section-link classes above */
.section{
   display:none;
}
.section_active{
   display:block;
   padding:10px;
}


When the page loads, you might want to load the first section, so you may want to change your body tag as follows:

Code:
<body onload="displaySection(0);">


You may also want to be able to pass a link to a friend so that a section other than the starter tab (0) appears. To do this, we will need to invoke a new function again from the body tag. The function we need is:

Code:
function displaySectionFromURI(){
   var sectionNumber=unescape(location).split("?section=")[1];
   if(!isNaN(sectionNumber)){ if(sections[sectionNumber]){ displaySection(sectionNumber); } }
}


Then just make sure the body tag looks something like this:

Code:
<body onload="displaySection(0);displaySectionFromURI();">


That way, you can send a link like pagename.html?section=2 to load the "Company Background" page for example.

I hope this has been informative. I understand that it is very technical, but it will give you something to work with for a while. Of course, as this is a forum, I want you to reply and tell me if you have any problems or if you have added to it in some way.

Update: An example of a site I made using this code:

Spoiler for index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<meta name="author" content="Aaron Cunnington" />
<title>Kirkpatrick House</title>
<script type="text/javascript" src="kirkpatrick.js"></script>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body onload="displaySection(0);checkSectionFromURI();">
<div align="center"><div style="width:700px;">
<div id="menu"><script type="text/javascript"><!--
for(var i=0;i<menu.length;i++){
document.write("<a href=\"javascript:displaySection("+i+");\" class=\"section-link\" id=\"section-link_"+i+"\">"+menu[i]+"</a>");
}
//--></script></div>
<div id="main">
<!-- sections below -->
<div id="section_welcome-to-kirkpatrick-house" class="section">
<div align="center"><img src="images/gathering.jpg" alt="Kirkpatrick House" /></div>
<p>Welcome to our Kirkpatrick House Bed &amp; Breakfast website.</p>
</div>
<div id="section_make-a-reservation" class="section">
<p>Make a reservation here.</p>
</div>
<div id="section_contact-information" class="section">
<p>Contact information here.</p>
</div>
<!-- sections above -->
</div>
</div></div>
</body>
</html>

Spoiler for kirkpatrick.js:
// do not change any code below
function grab(o){
return document.all?document.all[o]:document.getElementById?document.getElementById(o):"";
}

function parseLink(s){
return s.replace(/\ /g,"-").toLowerCase();
}

function displaySection(n){
for(i=0;i<menu.length;i++){
var sectionLink=grab("section-link_"+i);
var sectionDiv=grab("section_"+parseLink(menu[i]));
sectionLink.className=i==n?"section-link_active":"section-link";
sectionDiv.className=i==n?"section-active":"section";
}
}

function checkSectionFromURI(){
var sectionNumber=unescape(location).split("?section=")[1];
if(!isNaN(sectionNumber)){
if(menu[sectionNumber]){
displaySection(sectionNumber);
}
}
}
// do not change any code above
///////////////////////////////////////////////////////////////////////////////////////////////////
// you can add menu items and sections below
var menu=[
"Welcome to Kirkpatrick House",
"Make a reservation",
"Contact information"
];
// you can add menu items and sections above

Spoiler for style.css:
body{
margin:10px;
background-color:#cf9;
background-image:url(images/bg/body.gif);
background-position:top;
background-repeat:repeat-x;
font:9pt segoe ui,verdana;
}

#menu{
border:1px inset #000;
border-width:0 0 1px 0;
padding:10px;
}
#menu a{
padding:10px;
margin:0 10px 0 10px;
color:#000;
text-decoration:none;
}
.section-link{
background-color:#9c6;
border:1px inset #000;
}
.section-link_active{
background-color:#fff;
border:1px outset #000;
border-bottom-color:#fff; border-bottom-style:solid;
}

.section{
display:none;
}
.section-active{
display:block;
padding:10px;
background-color:#fff;
border:1px outset #000;
border-width:0 1px 1px 1px;
text-align:left;
}


Update note for admins: I already provided this update but it seems it was reverted back to the original post. Can you look in to why this happened?

_________________
Follow your heart and live the dream <3


Mon Jun 29, 2009 4:38 am 
 Profile E-mail  
 
 Post Post subject: Re: JavaScript Navigation, Sectioning and Consistency
 
Offline
Monster Poster
Monster Poster
Years of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membership

Joined: Tue Jun 02, 2009 3:23 pm
Posts: 733
Karma: -19

Steam Login Name: BartmanTaz
Interesting. I may have use of this later.

_________________
Techious Star Community Member 2009/2010.

Voted for and funded by Scottie UK Ltd.


Thu Aug 13, 2009 12:42 pm 
 Profile E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Karma functions powered by Karma MOD © 2007, 2009 m157y