I am trying to use Javascript to emulate the CSS :target
pseudo-class so as to capture all events that result in an element on page being targeted. I've identified 3 trigger events:
window.location.hash
already targets an element of the same ID on initialisation- An anchor targeting the element is clicked
- The
hashchange
event is fired independently of the above (for example via thewindow.history
API)
Scenario 2 is important as a distinct case since I would want to invoke the click
event's preventDefault
. The simplified code for this scenario follows:
$('body').on('click', 'a[href*=#]', function filterTarget(clickEvent){
$(this.hash).trigger('target', [clickEvent]);
});
The problem comes when trying to implement scenario 3:
$(window).on('hashchange', function filterTarget(hashChangeEvent){
$(this.hash).trigger('target', [hashChangeEvent]);
});
If a target
handler doesn't cancel the native behaviour for scenario 2, it will be triggered again when the native behaviour causes the resulting hashchange
event. How can I filter out these edge cases?
POST-SOLUTION EDIT:
roasted's answer held the key — handle a namespaced hashchange event, then unbind and rebind the handler based on logic handled inside the click handler and its preventDefault. I wrote up the full plugin here.