Create a tab using Ajax

Source: Internet
Author: User

CSS tabs seem to be all the rage with the kids these days. I love the idea of them. it seems that the most popular method of creating tabs using CSS is the sliding doors method. they seem to look the coolest and are very easy to implement. however, they're so... static.

I 've taken the idea of the sliding door tabs and mixed them up with a little JavaScript and Ajax to make them very dynamic on the client side.

Maybe the first thing you ought to do is to take a look at this demo so you know what I'm talking about.

Some of the features you get with these Ajax tabs:

  • Easy to implement dynamic CSS tabs. the user can add and remove tabs (and content) without refreshing the page.
  • The ability to specify default open tabs
  • The ability for the website owner to specify which tabs can and cannot be closed after they are opened
  • The ability to assign a different color to each tab. This makes it easier to 'group' tabs with similar content.
  • Ajax preloading goodness. When a tab is opened, the contents of the tab's panel are loaded with Ajax.
  • You can open a tab while the user is focused on a different tab. the newly opened tab will preload it's content in the background and wait for the user to click the new tab when he or she is ready.
  • Tab events. Each tab can have it's own JavaScript code that will be triggered when a certain event happens. such as open, close, blur and focus.
  • Tested to work with Firefox 1.0, 1.5 and IE 6

Take a look at the Code:

  • Tabs. js
  • Ajax. js
  • Tabs.css

Download all the files needed for the demo. (because this demo uses Ajax, you'll need to put these files on a webserver. they won't run locally on your machine unless your workstation has a webserver running .)

To get started, you need some HTML:

Pretty simple stuff. you'll notice that when the page is opened, the function opentab () gets called. that's the function that opens up the first tab. take a look at the tabs. JS documentation below to see what all the arguments for opentab do.

Tabs. js functions:
Opentab (Tabid, tablabel, taburl, tabiscloseable, tabcolor)

This function creates a new tab or focuses on a tab if it already exists.
Tabid is a string that you create to give to this tab so that it can be accessed later.
Tablabel is a string that is displayed on the tab itself.
Taburl is a string the URL to the page that contains the content that will be loaded with Ajax.
Tabiscloseable is a Boolean (true or false) that specifies if the tab will have the "X" button that can close itself.
Tabcolor is a string that specifies which CSS groups to use for this tab and panel.

Createnewtab (Tabid, tablabel, taburl, tabiscloseable, tabcolor)

You shoshould never need to directly call this function, as opentab will call it if it needs to. The arguments are them same as opentab ().

Settaburl (Tabid, URL)

This function allows you to change the URL to the tab's content after you have already opened the tab. Changing
URL also refreshes the content.
Tabid is the Tabid of the tab you set when you created it.
URL is the URL of the content.

Closetab (Tabid)

Closes the tab that has the ID: Tabid

Getfocusedtabid ()

Returns the Tabid of the tab that is currently focused

Focustab (Tabid)

Changes the focus to the tab with tab ID: Tabid.

Refreshtab (Tabid)

Refreshes the content of the tab with tab ID: Tabid

Tabexists (Tabid)

Returns true or false depending on if a tab with Tabid exists.

Callback events

If the page that one of the tabs opens contains a hidden Div tag with specially formatted JavaScript Functions,
Those functions will be run by the Ajax tabs. Take a look at the demo's page #3 source below:

<div><div id="script_tab_page3" style="display:none;">tabOpentab_page3 = function() { alert("You opened page #3"); };tabClosetab_page3 = function() { return confirm("Are you sure you want to close this tab?"); };tabFocustab_page3 = function() { alert("Page #3 focused"); };tabBlurtab_page3 = function() { alert("Page #3 blured"); };</div>Page 3<br/><a href="javascript:void(0)" onclick="RefreshTab('tab_page3');">Refresh This Tab</a></div>

The name of the function for the open event wocould be: tabopen [Tabid]. replace the [Tabid] part with the tabs actual Tabid. page #3's Tabid is tab_page3 so that's why the OPEN function's name is tabopentab_page3. why put this code in a div tag instead of a script tag? Well, because IE doesn't handle it very well. You can't reference a script element with an ID in IE.

Well that's it. If I left anything out or something doesn't make sense, let me know. If anyone tests it in
Browser besides Firefox or IE, let me know how it works.

Http://www.havocstudios.com/ajaxTabs/demo.html view results
Today, we found that we have seen the actual use of this example on other websites.
After changing the code, click the button event and load it from the outside to the IFRAME webpage. The page is highly scalable and fast to load. You don't need to load n pages at the same time. When necessary, click the corresponding page, click to view the effect. http://www.sft800.com/tab/demo.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>
<Title> havocstudios: Ajax tabs demo </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Meta name = "Description" content = "demo of havocstudios Ajax tabs">
<Meta name = "keywords" content = "tabs Ajax CSS">
<Style type = "text/CSS" Media = "screen"> @ import "tabs.css ";
</Style>
<SCRIPT src = "Ajax. js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT src = "tabs. js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
Function loadpage_1 (){
Opentab ("tab_page1", "page 1", "page1.html", false ,'');
}
Function loadpage_2 (){
Opentab ("tab_page2", "page 2", "page2.html", false ,'');
}
Function loadpage_3 (){
Opentab ("tab_page3", "Page 3", "page3.html", false ,'');
}

</SCRIPT>
</Head>
<Body>
<Div id = "tabcontainer" onmouseover = "This. style. cursor = 'hand'">
<Div id = "tabs" onmouseover = "This. style. cursor = 'hand'">
<Ul id = "tablist" onclick = "loadpage_1 ()">
<Input id = "button1" type = "button" value = "button" name = "button1" onclick = "loadpage_1 ()">
<Input id = "button2" type = "button" value = "button" name = "button1" onclick = "loadpage_2 ()">
<Input id = "button3" type = "button" value = "button" name = "button1" onclick = "loadpage_3 ()">
</Ul>
</Div>
<Div id = "tabpanels" onmouseover = "This. style. cursor = 'hand'"> <font face = ""> </font> </div>
</Div>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.