Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 2.30 KB  |  hits: 25  |  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. Jquery Tool: Keep selected tab on refresh or save data
  2. $(function() {
  3.     // setup ul.tabs to work as tabs for each div directly under div.panes
  4.     $("ul.tabs").tabs("div.panes > div");
  5. });
  6.        
  7. function getCookie(c_name) {
  8.     var i, x, y, ARRcookies = document.cookie.split(";");
  9.     for (i = 0; i < ARRcookies.length; i++) {
  10.         x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
  11.         y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
  12.         x = x.replace(/^s+|s+$/g, "");
  13.         if (x == c_name) {
  14.             return unescape(y);
  15.         }
  16.     }
  17. }
  18.  
  19. function setCookie(c_name, value, exdays) {
  20.     var exdate = new Date();
  21.     exdate.setDate(exdate.getDate() + exdays);
  22.     var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
  23.     document.cookie = c_name + "=" + c_value;
  24. }
  25.        
  26. $(function() {
  27.    // retrieve cookie value on page load
  28.    var $tabs = $('ul.tabs').tabs();
  29.    $tabs.tabs('select', getCookie("selectedtab"));
  30.  
  31.    // set cookie on tab select
  32.    $("ul.tabs").bind('tabsselect', function (event, ui) {
  33.       setCookie("selectedtab", ui.index + 1, 365);
  34.    });
  35. });
  36.        
  37. $(function() {
  38.    //instantiate tabs object
  39.    $("ul.tabs").tabs("div.panes > div");
  40.  
  41.    // get handle to the api (must have been constructed before this call)
  42.    var api = $("ul.tabs").data("tabs");
  43.  
  44.    // set cookie when tabs are clicked
  45.    api.onClick(function(e, index) {
  46.           setCookie("selectedtab", index + 1, 365);
  47.    });    
  48.    // retrieve cookie value on page load
  49.    var selectedTab = getCookie("selectedtab");
  50.  
  51.    if (selectedTab != "undefined") {
  52.     api.click( parseInt(selectedTab) ); // must parse string to int for api to work
  53.    }
  54.  
  55. });
  56.  
  57. function getCookie(c_name) {
  58.     var i, x, y, ARRcookies = document.cookie.split(";");
  59.     for (i = 0; i < ARRcookies.length; i++) {
  60.         x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
  61.         y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
  62.         x = x.replace(/^s+|s+$/g, "");
  63.         if (x == c_name) {
  64.             return unescape(y);
  65.         }
  66.     }
  67. }
  68.  
  69. function setCookie(c_name, value, exdays) {
  70.     var exdate = new Date();
  71.     exdate.setDate(exdate.getDate() + exdays);
  72.     var c_value = escape(value) + ((exdays === null) ? "" : "; expires=" + exdate.toUTCString());
  73.     document.cookie = c_name + "=" + c_value;
  74. }