Click here to Skip to main content
Licence 
First Posted 18 Sep 2003
Views 226,325
Bookmarked 56 times

DHTML Web Tab Control

By | 18 Sep 2003 | Article
A simple client-side dynamic web tab control.

Sample screenshot

Introduction

This article describes how to implement a simple client-side web tab control using HTML, JavaScript and CSS. The web tab control is highly configurable, allowing content to be specified from local or remote sources, specifying a default selected tab upon loading the page and customizing the look & feel of it.

Design

Essentially, the web tab control is composed of HTML buttons for the tabs and an <IFRAME> for the main content area:

Design

The HTML button tabs and <IFRAME> main content area are separated into two <DIV> container objects so that, upon resizing the browser window, the web tab control maintains structure, avoiding overlapping that may possibly be caused by positioning attributes.

Configuration

The tabs and their respective local/remote content sources are specified in an array; each string item in the array represents a tab and details of it separated by a “|” pipe character. Note that each string item is followed by a “,” comma, except the last item:

var tabs = new Array
(
    "MSDN|http://msdn.microsoft.com",
    "CNN|http://www.cnn.com",
    "NASA|http://www.nasa.gov",  
    "Google|http://www.google.com|*",
    "Forbes|http://www.forbes.com"
);

In general, each tab item in the array of tab string items, follows this template:

“Tab-text | URI | [*]” 
  • Tab-text: This is the first part of each item which represents the text that appears on a tab.
  • URI: This is the second part after the first “|” pipe character and is the tab’s respective URI content source.

Optionally, a second “|” pipe character followed by a “*” wildcard character after specifying the URI will make that specific tab, a default selected tab when the page loads:

"Google|http://www.google.com|*"

Finally, the <DIV> container objects are declared in the body of the web page:

<DIV ID="divTabButtons"></DIV>
<DIV ID="divTabFrame"></DIV>

…and the web tab control initialized via calling the tabLoad method:

<BODY onLoad="tabLoad()">

Look & Feel

The look & feel of the web tab control is adjusted entirely through the use of CSS. The <IFRAME> main tab content area design is specified by the .tabFrame style:

.tabFrame
{
    ORDER: 0;
    HEIGHT: 90%;
    WIDTH: 100%;
    BORDER-TOP: #93BEE2 9PX SOLID;
    BORDER-BOTTOM: #93BEE2 9PX SOLID;
    BORDER-LEFT: #93BEE2 9PX SOLID;
    BORDER-RIGHT: #93BEE2 9PX SOLID;
    SCROLLBAR-FACE-COLOR:#6699CC;
    SCROLLBAR-HIGHLIGHT-COLOR:#FFFFFF;
    SCROLLBAR-SHADOW-COLOR:#6699CC;
    SCROLLBAR-ARROW-COLOR:#FFFFFF;
    SCROLLBAR-DARKSHADOW-COLOR:#6699CC;
}

In its default unselected state, a tab button’s design is defined by the .tabOff style:

.tabOff
{
    FONT-FAMILY: Verdana;
    FONT-SIZE: 11;   
    FONT-WEIGHT: 700;
    TEXT-ALIGN: CENTER;  
    COLOR: #003399;
    BACKGROUND-COLOR: #c4e0f0;  
    BORDER-BOTTOM: #c4e0f0 1PX SOLID;
    HEIGHT: 25;
    CURSOR: HAND;

}

When a tab button is clicked (tab selected), its design state is defined by the .tabOn style:

.tabOn
{    
    FONT-FAMILY: Verdana;
    FONT-SIZE: 11;    
    FONT-WEIGHT: 700;
    TEXT-ALIGN: CENTER;
    COLOR: #003399;
    BACKGROUND-COLOR: #93BEE2;
    BORDER-BOTTOM: #93BEE2 1PX SOLID;    
    HEIGHT: 35;
    CURSOR: HAND;
}

The BORDER-TOP color style attribute of the <IFRAME> main tab content area .tabFrame has to be (well, works best) the same as the BORDER-BOTTOM color style attribute of the tab buttons when they are selected, .tabOn. This is so that when the tab button is selected, it will look as part of the <IFRAME> and provide a continuous and consistent look. Apart from changing colors, another difference between the .tabOn and .tabOff styles is the height of the tab button; when selected .tabOn, a tab’s height is increased, yielding it with a look of self-importance.

Align

By means of setting a global tabAlign variable, the tabs can be aligned RIGHT, CENTER or LEFT as illustrated above.

Implementation

The tabLoad method when called, creates and initializes the web tab control. First it aligns the tabs:

HTML += "<P ALIGN="+tabAlign+">";

Then for every element in the array of string items representing information about a tab, it splits the string at each “|” pipe character extracting parts of the string and creates a HTML button with the .tabOff style and assigning them with a unique ID as well as an onClick event handled by a tabOnClick method:

for (var i = 0; i < tabs.length; i++)
{
    var tab = tabs[i].split("|");
    HTML += "<INPUT TYPE='BUTTON' ID="+i+" 
            CLASS='tabOff' VALUE="+tab[0]+" 
            onClick='tabOnClick("+i+")'> ";
}

The tab buttons are then bound to the divTabButtons <DIV> container object:

divTabButtons.innerHTML = HTML;

Finally, the tabLoad method seeks out which array string item (which represents a tab and its respective information) contains a “*” wildcard character by further splitting the string at the next “|” pipe character. If found, it calls the tabOnClick method passing to it, the ID of the corresponding tab button created earlier:

for (var i = 0; i < tabs.length; i++)
{    
    var tab = tabs[i].split("|");
    
    if (tab[2] == "*")
    {
        tabOnClick(i);
        
        break; 
    }
}

The tabOnClick method has a parameter which specifies the ID of a tab button. When called, it first sets all the tab button styles to the default .tabOff style:

var oElement = null;

for (var i = 0; i < tabs.length; i++)
{    
    oElement = document.getElementById(i);
    oElement.className = "tabOff";
}

Then, with the aid of its parameter which represents the ID of a specified tab button, it sets the specified tab button’s style to .tabOn:

oElement = document.getElementById(ID);
oElement.className = "tabOn";

Finally, the tabOnClick button seeks out the relevant URI content information of the specified tab from the array of tabs and setting the <IFRAME> SRC attribute to it for displaying, the <IFRAME> is then bound to the divTabFrame <DIV> container object:

var tab = tabs[ID].split("|");
divTabFrame.innerHTML = "<IFRAME SRC="+tab[1]+ 
         " CLASS='tabFrame'></IFRAME>";

As a finishing touch, just so that the selected tab button doesn’t look so much as a button, the document body is told to steal the focus:

document.body.focus();

Notes

  • Tested only with IE6.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Steve Puri



United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPosition of tab frame PinmemberSarah Riblon9:52 6 Aug '09  
GeneralGood Artical Pinmemberparasaniasandip20:33 15 Sep '08  
GeneralGreat Idea, pointer to FireFox compatible solution PinmemberTayBill28:18 7 Jul '07  
Questionrefreshing within frames Pinmemberhaskillg8:47 30 Mar '07  
GeneralMind Blowing PinmemberJitku21:27 15 Oct '06  
GeneralSTOP RELOADING PinmemberDelver8:32 7 Jun '06  
JokeThe best one! PinmemberBlinZaparaSEtimiImenami0:10 19 Feb '06  
GeneralRe: The best one! PinmemberSteve Puri8:37 19 Feb '06  
GeneralRe: The best one! PinmemberBlinZaparaSEtimiImenami23:39 19 Feb '06  
GeneralJavaScript Pinmembercasimirrex6:58 19 Nov '05  
Dear Programmers,
Good morning to all.In my project i've to inclde a tab contols ,so thats why i refered a Wrox Publications matrials for me.But in this coding click event has not accord.Even in JavaScript area is new for me.So can any bear a hand to me?In this mail that i've been attached the coding too.i'm keen on awaiting the any one response from you.
 
peace out
regards
rex
-------------------------------
 
<html>
<head>
<title>Example Tabs</title>
<style>
.active
{
color:#800000;
cursorointer;
}
.inactive
{
color:000080;
cursor:default;
}
 
table
{
border-width:2px;
border-style:solid;
border-color:#707070;
}
th,td
{
text-align:center;
color:#0000FF;
}
.inactiveTab
{
border-bottom-width:1px;
border-bottom-style:solid;
border-bottom-color:#707070;
border-right-width:1px;
border-right-style:solid;
border-right-color:#707070;
padding-left:3px;
background-color:#ffd0d0;
}
.activeTab
{
border-bottom-width:1px;
border-bottom-style:solid;
border-bottom-color:#707070;
border-right-width:1px;
border-right-style:solid;
border-right-color:#707070;
padding-left:3px;
background-color:#d0ffd0;
}
div
{
display:table-row;
}
</style>
<script>
function clearTabs()
{
var tabs = document.getElementByID("tabBar").cells
for(var i=0; i
 
</head>
<body>










Details

Search

help


Details


Some information can be presented here.



Help



  1. Example of Tabs



</body>
</html>

GeneralMultiple words in title Pinmembergogetsome7:32 22 Sep '05  
GeneralRe: Multiple words in title PinmemberSteve Puri8:42 22 Sep '05  
GeneralRe: Multiple words in title PinmemberBlinZaparaSEtimiImenami23:38 19 Feb '06  
GeneralRe: Multiple words in title Pinmembergogetsome2:40 20 Feb '06  
GeneralSupport for other browsers Pinmemberjaemmer3:59 16 Sep '05  
Questionenhancement? PinmemberAnoopB4:42 15 Oct '04  
AnswerRe: enhancement? PinmemberAnoopB7:49 15 Oct '04  
GeneralRe: enhancement? PinmemberSteve Puri9:12 16 Oct '04  
GeneralRe: enhancement? PinmemberAnoopB3:18 18 Oct '04  
QuestionHow to pass variables between frames Pinmemberkishor kumar18:07 11 May '04  
GeneralUsability PinmemberStephane Rodriguez.20:46 19 Sep '03  
GeneralRe: Usability PinmemberSteve Puri22:02 19 Sep '03  
GeneralRe: Usability PinmemberStephane Rodriguez.1:06 20 Sep '03  
GeneralRe: Usability PinmembertheJazzyBrain1:27 15 Jun '04  
GeneralRe: Usability Pinmemberkishor kumar23:43 6 Oct '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 19 Sep 2003
Article Copyright 2003 by Steve Puri
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid