I am trying to use Javascript to emulate the CSS :target pseudo- :target to catch all the events that result in such an element on the page. I defined three trigger events:
window.location.hash already targeting an element with the same identifier during initialization- Snap to snap element
- The
hashchange event is hashchange regardless of the above (for example, through the window.history API)
Scenario 2 is important as a separate case, since I would like to trigger the click preventDefault event. The following is simplified code for this scenario:
$('body').on('click', 'a[href*=#]', function filterTarget(clickEvent){ $(this.hash).trigger('target', [clickEvent]); });
The problem occurs when trying to implement scenario 3:
$(window).on('hashchange', function filterTarget(hashChangeEvent){ $(this.hash).trigger('target', [hashChangeEvent]); });
If the target handler does not cancel the native behavior for script 2, it will be run again when the native behavior raises the resulting hashchange event. How can I filter out these edge cases?
CHANGE POST SOLUTIONS:
the fried response held the key - it processed the hashchange event with names, then untied and rewrote the handler based on the logic processed inside the click handler and its prevention Default. I wrote a complete plugin here .
source share