Created
September 10, 2014 22:58
-
-
Save kklai/d68003b693fdc1ed0499 to your computer and use it in GitHub Desktop.
Update page url on click
This file contains hidden or 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
<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