Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1385
  • Last Modified:

Using jQuery UI tabs cookie option

I have some jquery ui tabs on my page.  I want to use the cookie option to keep track of the tab a user was last on.  I can successfully set the cookie but can't figure out how to use that cookie to select a tab.

Here is what I have so far....  Do I have the getter and setter javascript in the correct spot?

 
<script type="text/javascript">
$(document).ready(function() {
    $("#tabs").tabs({
        cookie: {
            name: 'tab_cookie',
            expires: 30
        }
    });
//getter
var cookie = $( "#tabs" ).tabs( "option", "cookie" );
//setter
$( "#tabs" ).tabs( "option", "cookie", { expires: 30 } );
});
</script>


<!--- TABS --->
<div id="tabs">
			<ul>
				<li><a href="#tabs-1">TAB 1</a></li>
				<li><a href="#tabs-2">TAB 2</a></li>			
			</ul>
							
			<div id="tabs-1">tabs 1 content</div>
			<div id="tabs-2">tab 2 content</div>			  
				
</div>

Open in new window

0
MFredin
Asked:
MFredin
  • 4
  • 4
1 Solution
 
MFredinAuthor Commented:
Forgot to mention... documentation is here:  http://jqueryui.com/demos/tabs/#option-cookie
0
 
RobOwner (Aidellio)Commented:
I'm just looking through the documentation and it should automatically set and get the cookie without you having to do it manually

in other words remove:

//getter
var cookie = $( "#tabs" ).tabs( "option", "cookie" );
//setter
$( "#tabs" ).tabs( "option", "cookie", { expires: 30 } );
});

The other thing is it is not in the document.ready function but is executed as part of the general code so if it still doesn't work after removing your setter and getter then replace the document.ready with:

      <script>
            $(function() {
                  $( "#tabs" ).tabs({
                        cookie: {
                              // store cookie for a day, without, it would be a session cookie
                              expires: 1
                        }
                  });
            });
      </script>
0
 
MFredinAuthor Commented:
I don't think it does... I tried it both ways and no go.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RobOwner (Aidellio)Commented:
Here is a working version, I think you were just missing the cookie plugin? http://plugins.jquery.com/project/Cookie

<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel="stylesheet" href="jquery-ui.css" type="text/css" media="all">
<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js"></script>
<script language="JavaScript" type="text/javascript" src="jquery.cookie.js"></script>


</head>

<body>
<script type="text/javascript">
	$(function() {
		$("#tabs").tabs({
			cookie: {
				name: 'tab_cookie',
				expires: 30
			}
		});
		//getter
		//var cookie = $("#tabs").tabs("option", "cookie");
		//setter
		//$("#tabs").tabs("option", "cookie", {expires: 30});
	});
</script>
<!--- TABS --->
<div id="tabs">
   <ul>
      <li><a href="#tabs-1">Nunc tincidunt</a></li>
      <li><a href="#tabs-2">Proin dolor</a></li>
      <li><a href="#tabs-3">Aenean lacinia</a></li>
   </ul>
   <div id="tabs-1">
      <p>Tab 1 content</p>
   </div>
   <div id="tabs-2">
      <p>Tab 2 content</p>
   </div>
   <div id="tabs-3">
      <p>Tab 3 content</p>
   </div>
</div>

</body>
</html>

Open in new window

0
 
RobOwner (Aidellio)Commented:
You also don't need the getter and setter function if all you want to do is remember the tab.  that's more if you want to use the value of the cookie for other reasons eg store in a database
0
 
MFredinAuthor Commented:
Here is what I have for code.  When I load the page, the cookie is being set to '1', but it's not being updated when I change tabs.  Any ideas?
<script>
	$(function() {
		$( "#tabs" ).tabs({
			ajaxOptions: {
				error: function( xhr, status, index, anchor ) {
					$( anchor.hash ).html("Couldn't load this tab. We'll try to fix this as soon as possible. ");
				}
			}
		});
	
		$("#tabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
		$("#tabs li").removeClass('ui-corner-top').addClass('ui-corner-left');
		$("#tabs").tabs({
			cookie: {
				name: 'tab_cookie',
				expires: 30
			}
		});
		//getter
		//var cookie = $("#tabs").tabs("option", "cookie");
		//setter
		//$("#tabs").tabs("option", "cookie", {expires: 30})
	});
	</script> 
	
<div id="tabs"> 
	<ul> 
		<li><a href="MyPosts/index.cfm">My Posts</a></li>
		<li><a href="Overview/index.cfm">Overview</a></li>
		<li><a href="MyRatings/index.cfm">My Ratings</a></li>
		<li><a href="MyProfile/index.cfm">My Profile</a></li>
		<li><a href="MyAccount/index.cfm">My Account</a></li>
		<li><a href="WebsiteSettings/index.cfm">Website Settings</a></li>
	</ul> 
</div>

Open in new window

0
 
MFredinAuthor Commented:
Got it... just needed to tweak the js to this:
$(function() {
        $("#tabs").tabs({
            ajaxOptions: {
                error: function(xhr, status, index, anchor) {
                    $(anchor.hash).html("Cannot load this tab at this time.");
                }
            },
            cookie: {name:'mytabcookiename',
                // store cookie for set time, without, it would be a session cookie
                expires: 30
            }
        });
		//getter
		//var cookie = $("#tabs").tabs("option", "cookie");
		//setter
		//$("#tabs").tabs("option", "cookie", {expires: 30})
    });

Open in new window

0
 
mbertuolCommented:
How do I set no expiration? I wan the cookie to be clear when whe user closes the browser.
0
 
RobOwner (Aidellio)Commented:
In future you should open a new question to answer this but the answer is in the header of the js file:

 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now