views:

294

answers:

4

As far as I know all popular web browsers execute the onclick attribute of an anchor link first, then follow the path specified by the href attribute.

The problem here is that the onclick attribute only gets called when clicking with the left mousebutton (without pressing ctrl or shift for a new tab or window) or when pressing enter on your keyboard while the tabIndex is set to the link you want to follow.

But there are many other ways of following a link than just those two.

  • ctrl + click
  • shift + click
  • rightmousebutton + open
  • rightmousebutton + new tab
  • drag & drop link to address bar

and so on...

My client uses onclick for conversion statistics. Which seems heavily unreliable.

My question: What percentage of hyperlinks are being followed without activating their onclick attribute?

All estimates are highly appreciated. I'm completely lost; I think it can be any number...

+1  A: 

It depends. If the audience is more technically inclined, I'd assume that using alternative ways of following a link would be more common. All in all, though, even a lot of technical people seem to be unaware of things like clicking the middle mouse button to open or close a tab. If that's the case with technical people, I wouldn't be surprised if almost no one in the general audience used it.

Reinis I.
A: 

What are you doing in your onclick handler that you are worried about not working? e.g. if you have a regular link to a page... and the onclick just opens that same page in a pre-sized popup for user convenience in a web application... then there's likely no issue (e.g. CTRL+Click) still opens the page, it just gets opened in a new tab)

If you are just trying to "log" every click in the site/application for tracking purposes then maybe you can hook into the onmousedown/up or focus/blur events instead/also.

scunliffe
It's (indirectly) being used for cost-per-click billing.
Erik
+5  A: 

Aside from those of us who habitually middle-click/ctrl-click to open links in new tabs, there's another major cause of onClick failure: NoScript and similar plugins which allow javascript to run only when it comes from whitelisted sites. If your domain isn't on my whitelist, then your onClick won't run, no matter how I trigger the link.

If you want reliable stats on which pages people are visiting, there's only one bulletproof source for that: The web server logs.

The logs are probably also your best bet for tracking how people move throughout the site, but they're not entirely reliable, as some privacy-paranoid users will falsify their referer headers or just not send them at all, but I expect that to be far less common than disabling javascript.

Dave Sherohman
The links are all exit-links. I was thinking of a PHP script that does the counting of clicks.But you're right. Even a 'safely configured' Internet Explorer can block the onlick attribute.
Erik
Webserver logs don't show pages hit via cache. If you're trying to analyze the behavior of people on your site, you want to see what pages the revisit, not just the pages they happen to visit for the first time.
kdgregory
+1 for logs, but some browsers fail to consistently report a valid referer for a variety of reasons - it's not just paranoid users.
Mayo
Well, for *exit* links the referrer is not important, right? To use one's own logs each link should redirect through some intermediate URL though.
Arjan
Add in the fun that if your page transition is via JavaScript and you are not directly accounting for it, IE will NOT send the referrer which makes it harder to track - ugh!
scunliffe
@kdgregory: I stand corrected. There is no bulletproof way to track what pages are visited.
Dave Sherohman
Isn't it easy to keep a whole page from being cached? I doubt people (or add-ons) are blocking any web bugs (http://en.wikipedia.org/wiki/Web_bug) if originating from the site they are visiting. Serve with `Cache-Control: no-cache` and the like, and I think you can track *all* page visits while still allowing caching of the HTML itself. Of course, in the log that only gives you IP addresses, not knowing if it is indeed the same visitor (cookies can still track the behaviour of most visitors). But it should suffice to track exit links (through some intermediate `302 Moved temporarily` page)?
Arjan
A: 

The links are all exit-links. I was thinking of a PHP script that does the counting of clicks.

Though this is not the subject of your question, you might want to have a look at Google Analytics then. They are The Master in tracking you. They track right-clicks (even when not actually selecting "open in new window" after that, which they obviously cannot know), which will yield Ajax calls to http://www.google.com/url.

To see Analytics in action: with Adblock and the like disabled, search something on google.com and open up Live HTTP Headers in Firefox (or your Firebug Net tab in Firefox, or WebKit's Resources pane in Safari or Chrome, sorted by time). Next, click, right-click, shift-click or control-click any search result (preferably a result that does not require a lot of HTTP traffic by itself, or is in your browser's cache).

the onclick attribute only gets called when clicking with the left mousebutton (without pressing ctrl or shift for a new tab or window)

Not entirely true. I created a quick test at JS Bin, to show that modifier keys do not affect the onclick event. (For right-click, one should use the oncontextmenu event.)

Arjan