Saturday, October 11, 2008

Ajax Tabs Script

This script can load content of external pages by Ajax when clicking on the tab buttons the script load external page without refreshing pages this script is simple to use and customizable

Author Home Page - Script Demo - ajaxtabscontent.zip - Installation

Setting up the script on your page- basic usage

Lets see in detail now how to set up this script on your page. After having added the .js and .css file references to the HEAD section of your page, as seen in demo.htm, proceed with the following:Step 1: Define the HTML for the Tabs themselves. This can be a fancy CSS menu, or even just a DIV tag with a bunch of links inside. Lets use the former:

<ul id=”countrytabs” class=”shadetabs”>


<li><a href=”#” class=”selected” rel=”#default”>Tab 1</a></li>

<li><a href=”external2.htm” rel=”countrycontainer”>Tab 2</a></li>


<li><a href=”external3.htm” rel=”countrycontainer”>Tab 3</a></li>

<li><a href=”external4.htm” rel=”#iframe”>Tab 4</a></li>


<li><a href=”http://www.dynamicdrive.com”>Dynamic Drive</a></li>

</ul>

The parts in red are significant, regardless of how you define your tabs:

  1. id=”countrytabs”: Give the main container of your tab links a unique ID (ie: “countrytabs“). All links (”A” elements) within this container will be scanned by the script for special meaning.
  2. rel=”#default|containerid|#iframe”: For each tab link that should reveal content when clicked on, add a “rel” attribute inside its link tag with one of three possible values: “#default“, id_of_container_div, or “#iframe“. This in combination with the link’s href attribute determines what external file to fetch, and how to display it. Most commonly you’ll be using containerid, which corresponds to the ID of the container DIV on the page in which to load the Ajax fetched content (see Step 2 below). “#default" and “#iframe” on the other hand are both special keywords. “#default" causes the tab to show no external content but instead what’s embedded directly inside the container DIV, while #iframe causes the tab to show its content inside an IFRAME tag instead of via Ajax.
  3. rev=”divid1, divid2, etc”: For each tab link, you can also insert a rev attribute to simultaneously toggle the visibility of arbitrary DIVs on the page. See example #1 in the documentation page for more info.
  4. class=”selected”: If you wish a certain tab to be selected by default when the page loads, give the tab in question a class="selected" assignment. By default it should be added inside the link itself (”A“). However, the script can also be told to look for such as class in the link’s parent container instead (in this case, “LI“). This is useful depending on how the CSS for your tabs are structured. See the documentation page for more info.
  5. A tab can also just act as a regular link, with the absence of the “rel” attribute (ie: the “Dynamic Drive” tab at the very end).

There is an additional attribute you can use, the “rev” attribute, to also expand/contract arbitrary DIVs outside the Tab interface. See the documentation below for details.

Step 2: Define a Content container DIV that will house the display of the external pages specified in the tabs above:

<div id=”countrydivcontainer” style=”border:1px solid gray; width:450px; margin-bottom: 1em; padding: 10px”>

//Optional default content here.

</div>

The only required portion is the DIV itself with a unique ID assigned as highlighted in red. The content “//Optional default content here” that’s added inside the DIV, is, well, optional. Define it if you wish a certain tab to show this content instead of external content fetched via Ajax or IFRAME. It’s useful as the content for the tab selected by default when the page loads, to cut down on the trips the script has to take to fetch files from the server. To associate a tab with the default content of the container DIV btw, you would use the declaration rel="#default" inside the tab’s link.

Step 3: With your Tabs and the container DIV both defined, it’s now time to activate the script, by calling ddajaxtabs() with the desired settings:

<script type=”text/javascript”>


var countries=new ddajaxtabs(”countrytabs“, “countrydivcontainer“)


countries.setpersist(true)

countries.setselectedClassTarget(”link”) //”link” or “linkparent”

countries.init()


</script>

The first line tells the script the IDs of both your CSS tabs container and content container, in that order, with the variable “countries” being an arbitrary but unique identifier variable. The second line toggles the persistence of the tabs’ state, whether or not a clicked on tab should be remembered and recalled upon the user’s return (within the same browser session). The 3rd line tells the script whether to look for class="selected" within the tab link (”A“) itself, or its parent container instead (ie: “LI“). What value to use depends on where you will be inserting the class="selected" attribute inside a tab. Lastly, the 4th line initializes everything.

And that’s it for setting up the script in its basic form. However, you’ll want to also learn how to expand/contract arbitrary DIVs on the page at the same time a tab is selected, plus how to get the tabs to automatically rotate like a slideshow. That’s what the documentation below is for!

Adding default content to Ajax Tabs

While you can certainly configure the script so that every tab loads an external page, you might as well have a particular tab show content that already exists within the content container:

<div id=”countrydivcontainer” style=”border:1px solid gray; width:450px; margin-bottom: 1em; padding: 10px”>


//Optional default content here.

</div>

Any HTML added inside the DIV above is the default content, and can either be shown if the script is not set to select any tab automatically, or if associated with a certain tab, so clicking that tabs shows this content. For the later, to associate the default content with a tab, just use the keyword “#default” instead of the ID of the container DIV inside the tab link’s “rel” attribute:

<li><a href="#" rel="#default“>Tab 1</a></li>

Syntax for specifying an external page to load

When specifying a tab to load an external page, either via Ajax or IFRAME, the path to the page is specified in the tab link’s “href” attribute:

<li><a href="external.htm” rel=”countrycontainer”>Tab 2</a></li>

This assumes external.htm is in the same directory as where the Ajax Tabs script is. You use an absolute URL to the external page as well:

<li><a href="http://www.mysite.com/dir/external.htm” rel=”countrycontainer”>DHTML</a></li>

For Ajax fetched pages, You cannot, however, specify a URL that’s outside your server’s domain, such as “http://www.google.com/page.htm’, as Ajax security dictates that the calling page and page to be fetched via Ajax must be from the same domain. This limitation doesn’t apply to the IFRAME method.

This convention for specifying the page to load means the script downgrades 100% with JS disabled browsers and search engines, allowing them to load the links as normal pages.

Turning on slideshow mode

You can have the tabs within your Ajax Tabs Content automatically rotate every x seconds until a user explicitly clicks on a tab. To enable “slideshow mode” on your Tab Content, just pass in an integer when calling instance.init() to initialize your script:

Notice the integer in red, which is in units of milliseconds. With one defined, the script will rotate through the tabs every x milliseconds (so 3000=3 seconds).

Labels: , ,



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home