Skip to content

Instantly share code, notes, and snippets.

@Koc
Created February 1, 2011 19:34
Show Gist options
  • Save Koc/806470 to your computer and use it in GitHub Desktop.
Save Koc/806470 to your computer and use it in GitHub Desktop.
function anchorEventer(baseUrl)
{
if (baseUrl) {
anchorEventer.oldUrl = baseUrl;
}
if (!anchorEventer.oldUrl) {
anchorEventer.oldUrl = document.location.href;
}
return {
observe : function() {
if (anchorEventer.worker) {
return false;
}
anchorEventer.worker = window.setInterval(function() {
if (anchorEventer.oldUrl != document.location.href) {
//console.log('old', oldUrl, 'new', document.location.href);
var oldUrl = anchorEventer.oldUrl;
anchorEventer.oldUrl = document.location.href;
$(document).trigger({
type : 'anchorChange',
hrefOld : oldUrl,
hrefNew : document.location.href,
hashOld : oldUrl.replace(/(.*?)(#|$)/, ''),
hashNew : document.location.href.replace(/(.*?)(#|$)/, '')
});
}
}, 50);
return true;
},
stopObserving : function()
{
clearInterval(anchorEventer.worker);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment