You are here: Reference > JavaScript > client-side > event handling > properties > isTrusted (event)

isTrusted property (event)

Browser support:
9
Returns a Boolean value that indicates whether the event is a trusted event or not.
In Firefox, an event is trusted if it is invoked by the user and not trusted if it is invoked by script. In Internet Explorer, all events are trusted except that are created with the createEvent method. See the example for details.

Syntax:

object.isTrusted;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Boolean that indicates whether the event is trusted or not.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the isTrusted property:
<head>
    <script type="text/javascript">
        function GetTrusted (event) {
            if ('isTrusted' in event) {
                if (event.isTrusted) {
                    alert ("The event is trusted.");
                }
                else {
                    alert ("The event is not trusted.");
                }
            } 
            else {
                alert ("Your browser does not support the isTrusted property!");
            }
        }

        function SimulateClick () {
            var button = document.getElementById ("myButton");
            button.click ();
        }

        function SimulateClickCreateEvent (event) {
            if (event.initMouseEvent) {     // all browsers except IE before version 9

                var clickEvent = document.createEvent ("MouseEvent");
                clickEvent.initMouseEvent ("click", true, true, window, 0, 
                                            event.screenX, event.screenY, event.clientX, event.clientY, 
                                            event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, 
                                            0, null);

                var button = document.getElementById ("myButton");
                button.dispatchEvent (clickEvent);
            } else {
                alert ("Your browser does not support the initMouseEvent method.");
            }
        }
    </script>
</head>
<body>
    Click on the following button to invoke an onclick event: 
    <button id="myButton" onclick="GetTrusted (event);">Invoke click</button>
    <br /><br />
    Click on the following button to simulate an onclick event by script.
    <button onclick="SimulateClick ();">Simulate click</button>
    <br /><br />
    Click on the following button to simulate an onclick event with the createEvent method.
    <button onclick="SimulateClickCreateEvent (event);">Simulate click with createEvent</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

External links:

User Contributed Comments

Post Content

Post Content