Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

jQuery Script

By: a guest on Jan 5th, 2013  |  syntax: None  |  size: 1.91 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function(){
  4.         //      Main function that shows and hides the requested tabs and their content
  5.         var set_tab = function(tab_container_id, tab_id){
  6.                 //      Remove class "active" from currently active tab
  7.                 $('#' + tab_container_id + ' ul li').removeClass('active');
  8.  
  9.                 //      Now add class "active" to the selected/clicked tab
  10.                 $('#' + tab_container_id + ' a[rel="'+tab_id+'"]').parent().addClass("active");
  11.  
  12.                 //      Hide contents for all the tabs.
  13.                 //      The '_content' part is merged with tab_container_id and the result
  14.                 //      is the content container ID.
  15.                 //      For example for the original tabs: tab_container_id + '_content' = original_tabs_content
  16.                 $('#' + tab_container_id + '_content .tab_content').hide();
  17.  
  18.                 //      Show the selected tab content
  19.                 $('#' + tab_container_id + '_content #' + tab_id).fadeIn();
  20.         }
  21.  
  22.         //      Function that gets the hash from URL
  23.         var get_hash = function(){
  24.                 if (window.location.hash) {
  25.                         //      Get the hash from URL
  26.                         var url = window.location.hash;
  27.  
  28.                         //      Remove the #
  29.                 var current_hash = url.substring(1);
  30.  
  31.                         //      Split the IDs with comma
  32.                         var current_hashes = current_hash.split(",");
  33.  
  34.                         //      Loop over the array and activate the tabs if more then one in URL hash
  35.                         $.each(current_hashes, function(i, v){
  36.                                 set_tab($('a[rel="'+v+'"]').parent().parent().parent().attr('id'), v);
  37.                         });
  38.                 }
  39.         }
  40.  
  41.         //      Called when page is first loaded or refreshed
  42.         get_hash();
  43.  
  44.         //      Looks for changes in the URL hash
  45.         $(window).bind('hashchange', function() {
  46.                 get_hash();
  47.         });
  48.  
  49.         //      Called when we click on the tab itself
  50.         $('.tabs_wrapper ul li').click(function() {
  51.                 var tab_id = $(this).children('a').attr('rel');
  52.  
  53.                 //      Update the hash in the url
  54.                 window.location.hash = tab_id;
  55.  
  56.                 //      Do nothing when tab is clicked
  57.                 return false;
  58.         });
  59. });
  60. </script>