Results 1 to 10 of 10

Thread: Tab Script - Select tab from hash tag

  1. #1
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default Tab Script - Select tab from hash tag

    I'm working on a dynamic tab menu (powered by jQuery) for my site. It works perfectly, but I want to make it so that it selects a certain tab depending what the hash in the URL is.

    So if the URL was #contact it would select the "Contact" tab and show the associated content.

    Here's what I have: [Live Demo]
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    	<title>X96 Design and Development</title>
    	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    	<style type="text/css" media="screen">
    body { margin:10px; padding:0; font:1em "trebuchet ms", verdana, arial, sans-serif; font-size:100%; }
    h1 { margin-bottom:2px; font-family:garamond, Georgia, "times new roman", times, serif;}
    div.container { margin:auto; width:800px; margin-bottom:10px;}
    textarea { width:80%;}
    fieldset { border:1px solid #ccc; padding:1em; margin:0; }
    legend { color:#ccc; font-size:120%; }
    input, textarea { font-family:arial, verdana; font-size:125%; padding:7px; border:1px solid #999; }
    label { display:block; margin-top:10px; } 
    img { margin:5px; }
    
    ul.tabNavigation {
    	list-style:none;
    	margin:0;
    	padding:0;
    }
    
    ul.tabNavigation li {
    	display:inline;
    }
    
    ul.tabNavigation li a {
    	padding:3px 15px;
    	background-color:#ededed;
    	color:#555;
    	text-decoration:none;
    	text-shadow:0 1px 0 #fff;
    }
    ul.tabNavigation li a:hover {
    	color:#000;
    	text-decoration:none;
    }
    
    ul.tabNavigation li a.selected, ul.tabNavigation li a.selected:hover {
    	background-color:#333;
    	color:#fff;
    	padding-top:7px;
    	text-shadow:0 -1px 0 #000;
    	text-decoration:none;
    }
    
    ul.tabNavigation li a:focus {
    	outline:0;
    }
    
    div.tabs > div {
    	padding:5px;
    	margin-top:3px;
    	border-top:3px solid #333;
    }
    
    div.tabs > div h2 {
    	margin-top:0;
    }
    #logo {
    	text-decoration:none;
    	background:transparent url(includes/logo.png) no-repeat scroll 0 0;
    	display:block;
    	width:436px;
    	height:93px;
    	text-indent:-9999em;
    }
        </style>
    	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    	<script type="text/javascript" charset="utf-8">
    		$(function () {
    			var tabContainers = $('div.tabs > div');
    			tabContainers.hide().filter(':first').show();
    			$('div.tabs ul.tabNavigation a').click(function () {
    				tabContainers.hide();
    				tabContainers.filter(this.hash).show();
    				$('div.tabs ul.tabNavigation a').removeClass('selected');
    				$(this).addClass('selected');
    				return false;
    			}).filter(':first').click();
    		});
    	</script>
    </head>
    <body>
    	<div class="container">
    		<a id="logo" href="#home">X96 Design and Development</a>
    		<div class="tabs">
    			<ul class="tabNavigation">
    				<li><a href="#home">Home Slice</a></li>
    				<li><a href="#portfolio">Our Work</a></li>
    				<li><a href="#contact">Contact</a></li>
    			</ul>
    			<div id="home">
    				<h1>Welcome.</h1>
    				<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    			</div>
    			<div id="portfolio">
    				<h1>What We've Done.</h1>
    				<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    			</div>
    			<div id="contact">
    				<h1>Get in Touch.</h1>
    				<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    			</div>
    		</div>
    	</div>
    </body>
    </html>
    Any help would be appreciated...

    // X96 \\
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  2. #2
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default

    I'm confused, is something not working. On your live demo when you click a tab it changes? I'm using FF if that helps.

  3. #3
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    Ya - it works perfectly fine; I just wanted to know how to get it to change via a URL parameter, like if the URL was index.html#contact then it would automatically select the contact tab, or if it was index.html#about it would automatically select the about tab, etc.

    It's mostly for SEO, but also if the page is refreshed.

    Hope this clears it up, I'm never very good at explaining things... :P

    // X96 \\
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  4. #4
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default

    Oh I get it haha. Neither am I, that's why I don't help much I always confuse myself while trying to give an answer unconfusing to the OP.

    Anyways, I'm not sure here. I tried to replicate this same thing and eventually decided on cookies until I realized that would make bookmarking not work. I also thought of other reasons that would make it hard to function and in the end abandoned AJAX, for now, in favor of normal linking.

    So now that I've told you why I can't help you , I'll say I hope you find your answer. If you do without the thread please post your solution here or PM it to me. I would be interested to know. If it's possible I'm sure someone on here will tell us how.

    Tim

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can probably work out the details for yourself. I think the property you are looking for is:

    Code:
    location.hash
    You would want to find that, well anytime, as its value is always available as soon as the page starts to load. But you would want to act upon it onload or document ready, that sort of thing.

    To get an idea of what it returns, paste this into the browser's address bar on a page with a hash (#whatever), and on one without one:

    Code:
    javascript:alert(location.hash)
    Hit enter.

    Any questions, feel free to ask.

    If I missed the point, let me know.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following 2 Users Say Thank You to jscheuer1 For This Useful Post:

    twQ (12-29-2009),X96 Web Design (12-29-2009)

  7. #6
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    Thank you!

    I got the page content to load, but the selected class isn't being applied to the menu items... I should probably be able to figure it out...

    Here's the JS I have now:
    Code:
    	<script type="text/javascript" charset="utf-8">
    		$(function () {
    			var tabContainers = $('div.tabs > div');
    			tabContainers.hide().filter(':first').show();
    			$('div.tabs ul.tabNavigation a').click(function () {
    				tabContainers.hide();
    				tabContainers.filter(this.hash).show();
    				$('div.tabs ul.tabNavigation a').removeClass('selected');
    				$(this).addClass('selected');
    				return false;
    			}).filter(':first').click();
    			var hashme = location.hash;
    			tabContainers.hide();
    			tabContainers.filter(location.hash).show();
    			$('div.tabs ul.tabNavigation a').removeClass('selected');
    			$('div.tabs ul.tabNavigation a' + hashme).addClass('selected');
    			return false;
    		});
    	</script>
    It loads the page content, but, as mentioned, I can't get the selected class the be applied automatically...

    Any help would be appreciated!

    // X96 \\
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  8. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think there may be one or more kinks yet to work out (like at least dealing with URL's with no hash). But, the a tag you want to select doesn't have an id of the hash - '#' (if it did, that part should work), it has an href ending in the hash. So, instead of:

    Code:
    $('div.tabs ul.tabNavigation a' + hashme).addClass('selected');
    You would want:

    Code:
    $('div.tabs ul.tabNavigation a[href$=' + hashme + ']').addClass('selected');
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. The Following User Says Thank You to jscheuer1 For This Useful Post:

    X96 Web Design (12-29-2009)

  10. #8
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    Wow... I can't believe I didn't figure that out! Thank you! Some days I just can't think strait...

    I'm not the best with Javascript; could you tell me what I'm doing wrong with this IF statement, please?
    Code:
    var hasme = location.hash;
    if(hashme == null){
        $('div.tabs ul.tabNavigation a:first').click();
    }
    Thank you for all your help!!

    // X96 \\
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  11. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, unless that's a typo:

    Code:
    var hasme = location.hash;
    if(hashme == null){
        $('div.tabs ul.tabNavigation a:first').click();
    }
    It's either in null, which should work for any 'falsy' value for the hash, but might not, as an empty hash might not be an empty string or other 'falsy' value. But if that part's OK, it would then have to be the:

    Code:
        $('div.tabs ul.tabNavigation a:first').click();
    part.

    To eliminate the first possibility, load a page with no hash, then paste this into the address bar:

    Code:
    javascript:alert(location.hash == null)
    If that doesn't come back true, the test you have will never work.

    But I'm currently voting for the typo hypothesis. However, who knows? The live page hasn't been updated, so I cannot be sure how the new code is being used.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  12. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I just tried my own test, you need:

    Code:
    javascript:alert(location.hash == '')
    or:

    Code:
    javascript:alert(location.hash === '')
    to get true. Comparing it to null, even with a type conversion operator like == just doesn't seem to work. Perhaps location.hash can be viewed as an object when compared to other objects (null is an object). In cases like that, the two objects must be the same object, not just identical in meaning and/or composition.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •