Search code examples
javascripthtmlhrefonmouseoveronmousedown

Is onClick Universal for Mouse buttons?


I notice Google use onMouseDown in their search results - for web tracking, seeing keyword and ranking etc.

I want to know which is better, onClick or onMouseDown - or do they both support all of the following: middle button, left click, right click (other buttons like Gamer mouse). Are they supported in all browsers including those on mobile phones, tablets and all other operating systems? It is paramount that the function loads first. HREF is left for SEO and other UI/UX benefits in case of failure of javascript.

<a href="http://www.site.com" onclick="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>

Or

<a href="http://www.site.com" onMousedown="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>

Or the seemingly obvious (I do not do this as every character for space matters for my client)

<a href="http://www.site.com" onclick="doMyFunctionFirst();" onMouseDown="doMyFunctionFirst();">
JavaScript loaded before the href URL
</a>

Solution

  • The main difference is that while onmousedown triggers regardless of which mouse-button was pressed, onclick triggers only for the left mouse button - which will be most likely what you want.