I've got some kind of loop here, explanation under code.
$(document).ready(function(){
var newHash = "";
$('a').click(function(event) {
var id = $(this).attr('href');
alert(id);
//Do something with selector with id that is clicked
window.location.hash = $(this).attr("href");
event.preventDefault();
});
$(window).bind('hashchange', function(){
newHash = window.location.hash.substring(1);
if (newHash) {
$("ul li." + newHash + " a").trigger("click");
};
});
$(window).trigger('hashchange');
})
<ul>
<li class="first"><a href="#first">first</a></li>
<li><a href="#second">second</a></li>
<li><a href="#third">third</a></li>
</ul>
I'm using ba-hashchange plugin by Ben Alman Here is live example -> EXTERNAL LINK ON MY SERVER
Notice when you click "#first" you got 2 alerts but when paste link with changed hash ->CHANGED HASH You got only one alert.
Its like a loop, when you click link you get alert from click and new hash which trigger from changing URL. So you got 2 alerts.
I want only 1 alert. In this case. You paste link its trigger link which supposed from hash name and you got 1 alert. And when you click link it change hash but not trigger it from changing this hash.