Browse By Name
HTMLCSSJavaScriptAppendix
You are here: Reference > JavaScript > client-side > event handling > events > onlosecapture

onlosecapture event | losecapture event

A A Font size Print Content Add new content Share Share
Browser support:
Occurs when the object loses the mouse capture.
To set an object to capture mouse events, use the setCapture method. To remove the mouse capture, use the releaseCapture method. For further details about the capturing mechanism, please see the page for the setCapture method.

How to register:

In HTML:
<ELEMENT onlosecapture="handler">

In JavaScript:
object.onlosecapture = handler;
object.attachEvent ("onlosecapture", handler);
You can find the related objects in the Supported by objects section below.
The event object is accessible to all event handlers in all browsers. The properties of the event object contain additional information about the current event. To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
For a complete list of events, see the page for Events in JavaScript.

Basic information:

Bubbles No
Cancelable No
Type Event

Actions that invoke the onlosecapture event:

  • If an object captures mouse events and the setCapture method is invoked on another element.
  • If an object captures mouse events and the releaseCapture method is invoked on it.
  • If an object captures mouse events and its owner window becomes inactive.

Example HTML code 1:

This example illustrates the capturing mechanism in Internet Explorer:
<head>
    <style>
        #square {
            position:absolute;
            left:200px;
            top:200px;
            width:20px;
            height:20px;
            background:red;
        }
    </style>
    <script>
        var capturing = false;

        function ToggleCapture () {
            var square = document.getElementById ("square");
            if (square.setCapture) {
                if (capturing) {
                    square.releaseCapture ();
                }
                else {
                    capturing = true;
                    square.setCapture ();
                }
            }
        }

        function OnLoseCapture () {
            capturing = false;
        }
        
        function MoveSquare () {
            if (capturing) {
                var square = document.getElementById ("square");
                square.style.left = event.clientX + "px";
                square.style.top = event.clientY + "px";
            }
        }
    </script>
</head>
<body onkeydown="ToggleCapture ();"> <br />
    <h3 style="text-align:center">Press any key to toggle capture!</h3>
    While mouse events are captured, the square follows the mouse.
    <div id="square" onmousemove="MoveSquare ();" onlosecapture="OnLoseCapture ()"></div>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates a cross-browser solution for the previous example:
<head>
    <style>
        #square {
            position:absolute;
            left:200px;
            top:200px;
            width:20px;
            height:20px;
            background:red;
        }
    </style>
    <script>
        var capturing = false;

        function ToggleCapture () {
            var square = document.getElementById ("square");
            if (capturing) {
                if (square.releaseCapture) {
                    square.releaseCapture ();
                }
                else {
                    if (document.body.removeEventListener) {
                        capturing = false;
                        window.removeEventListener ("mousemove", MoveSquare, true);
                    }
                }
            }
            else {
                if (square.setCapture) {
                    capturing = true;
                    square.setCapture ();
                }
                else {
                    if (document.body.addEventListener) {
                        capturing = true;
                        window.addEventListener ("mousemove", MoveSquare, true);
                    }
                }
            }
        }

        function OnLoseCapture () {
            capturing = false;
        }
        
        function MoveSquare (event) {
            if (capturing) {
                var square = document.getElementById ("square");
                square.style.left = event.clientX + "px";
                square.style.top = event.clientY + "px";
            }
        }
    </script>
</head>
<body onkeydown="ToggleCapture ();"> <br />
    <h3 style="text-align:center">Press any key to toggle capture!</h3>
    While mouse events are captured, the square follows the mouse.
    <div id="square" onmousemove="MoveSquare (event);" onlosecapture="OnLoseCapture ()"></div>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content