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

MFredinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.