Skip to content

Instantly share code, notes, and snippets.

@kklai
Created September 10, 2014 22:58
Show Gist options
  • Save kklai/d68003b693fdc1ed0499 to your computer and use it in GitHub Desktop.
Save kklai/d68003b693fdc1ed0499 to your computer and use it in GitHub Desktop.
Update page url on click
<html>
<head>
<title></title>
</head>
<body>
<button class="intro" data-attr="intro">INTRO</button>
<button class="second-story" data-attr="second-story">SECOND STORY</button>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$("button").on("click", function(){
url = window.location.href;
slug = $(this).data("attr");
if ( url.indexOf(slug) > -1 ) {
//do nothing
} else if ( url.indexOf("#") > -1 ) {
clean_url = url.split("#")[0]
location.href = clean_url + "#" + slug;
} else {
location.href = url + "#" + slug;
}
})
$(document).ready(function(){
var anchor = window.location.href;
var n = anchor.split("#")[1]
console.log(n)
if (n === "intro") {
$(".second-story").hide();
}
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment