Hacker Newsnew | comments | show | ask | jobs | submit login

I think he means don't make links automatically open in new tabs when you left click them, middle mouse or whatever will still open in a new tab.



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.

-----


How does that respond to middle-mouse-clicks? I use that for open-in-new-tab functionality all the time. There's no need to chord the mouse and keyboard together for it. Right-click->context menu is the same way.

-----


From my experience, middle mouse clicks do not trigger the handler. Here's a fiddle to test: http://jsfiddle.net/GTWfn/. Try middle-clicking that link vs. left clicking.

-----




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: