Last active
August 29, 2015 14:02
-
-
Save bocharsky-bw/a481d2d6c1b9b647d72c to your computer and use it in GitHub Desktop.
Show dynamic content by hash changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDefaultSelector() { | |
return '.home'; | |
} | |
function hashToSelector(hash) { | |
return hash.match(/^#.+/) | |
? hash.replace(/^#/, '.') | |
: getDefaultSelector() | |
; | |
} | |
/** | |
* Show/hide dynamic content by hash tag | |
*/ | |
function bootstrap(selector) { | |
// alert(selector); // debug | |
var elements = $(selector); | |
if (elements.length || selector == getDefaultSelector()) { | |
$('.dynamic').hide(); | |
elements.show(); | |
} | |
} | |
/** | |
* Waiting DOM ready | |
*/ | |
$(document).ready(function(){ | |
// alert('DOM'); | |
/** init on page load */ | |
var selector = hashToSelector(location.hash); | |
bootstrap(selector); | |
/** re-init on hash change */ | |
$(window).bind('hashchange', function() { | |
var selector = hashToSelector(location.hash); | |
bootstrap(selector); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment