Comment

Comment by kodablah

This is what a lot of websites seem to have a problem with. It's not that difficult to put an href in your anchor. Then in your click handler, simply check modifier keys (ctrl, alt, shift) and if none of them are pressed, return true. Otherwise, do your HTML5 history (or hashbang) URL change normally. Here's kinda how I've done it in CoffeeScript (though not sure if exactly right):

    linkClickHandler = (e) ->
        # can't have meta key
        if not e.originalEvent?.ctrlKey and not e.originalEvent?.altKey and not e.originalEvent?.shiftKey
            # defer the work so we can kill the link right away
            deferred = () =>
                href = $(e.target).attr 'href'
                if href?
                    #do something here like history.pushState
            setTimeout deferred, 0
            # stop bubbling and return false
            e.stopPropagation()
            return false
        else
            return true
This gives the users that want to save links and open them in other ways, but still have a good feel when left click only. Clicking on directories in Github source trees demonstrate this well.
parent kodablah 14 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton