Solved

Using jQuery UI tabs cookie option

Posted on 2010-11-13
9
1,358 Views
Last Modified: 2012-08-13
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
Comment
Question by:MFredin
  • 4
  • 4
9 Comments
 

Author Comment

by:MFredin
ID: 34129680
Forgot to mention... documentation is here:  http://jqueryui.com/demos/tabs/#option-cookie
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 34133041
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
 

Author Comment

by:MFredin
ID: 34234414
I don't think it does... I tried it both ways and no go.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
ID: 34504067
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
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 34504094
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
 

Author Comment

by:MFredin
ID: 34510845
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
 

Author Comment

by:MFredin
ID: 34511055
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
 

Expert Comment

by:mbertuol
ID: 34715657
How do I set no expiration? I wan the cookie to be clear when whe user closes the browser.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 34717176
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question