Detect if mousedown inside a function

Posted on 2006-05-18
Last Modified: 2008-03-03
Is there anyway to detect mousedown inside of a javascript function?
Question by:johnywhite
    LVL 3

    Assisted Solution

    If you want a document to capture events at all times while the document is loaded, you could execute the:


    hope this helps
    LVL 28

    Accepted Solution

    Here is an example, which tracks the mouse event via only one function mH

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>

    <script language="javascript">
    function BrowserType () {
          var srchText = navigator.userAgent;
          var brwTypes = ("Opera,MSIE,Netscape,Firefox").split(',');
          for (var ix=0; ix < brwTypes.length; ix++) {
                if (srchText.toString().match(brwTypes[ix])) {
                      return brwTypes[ix];
          return null;

    function mH(evt) {
          var brwType = BrowserType();
          var btn = null;
          if (!evt) { evt = window.event; }
          if (evt.type.match ('mousemove')) {
             window.status = 'X ' + evt.clientX + ' Y ' + evt.clientY;
          if (evt.type.match('mouseup')) {
             window.status = 'Mouse Up';

          switch (brwType) {
          case 'MSIE':
                btn = evt.button;
          case 'Netscape':
          case 'Firefox':
                btn = evt.which;
                btn = evt.button;
          if (btn == 1) {
                window.status = evt.type + ' Left Button ';
          else if (btn == 2) {
                window.status = evt.type + ' Right Button ';
          else if (btn == 3) {
                window.status = evt.type + ' Right Button ';
    document.onmousedown = mH;
    document.onmousemove = mH;
    document.onmouseup = mH;

    <textarea cols="40" rows="10">

    Author Comment

    Thanks that will do for what I need.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now