Skip to content

Latest commit

 

History

History
57 lines (50 loc) · 2.35 KB

handling-windows-runtime-events-in-javascript.md

File metadata and controls

57 lines (50 loc) · 2.35 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Handling Windows Runtime Events in JavaScript | Microsoft Docs
01/18/2017
microsoft-edge
windows-integration
article
JavaScript, Windows Runtime events
Windows Runtime events [JavaScript]
d9436aff-2c30-4846-b8df-eaa3e63fd75c
6
MSEdgeTeam
msedgedevrel

Handling Windows Runtime Events in JavaScript

Windows Runtime events are not represented in the same way in JavaScript as they are in C++ or the .NET Framework. They are not class properties, but rather are represented as (lowercased) string identifiers that are passed to the class's addEventListener and removeEventListener methods. For example, you can add an event handler for the Geolocator.PositionChanged event by passing the string "positionchanged" to the Geolocator.addEventListener method:

var locator = new Windows.Devices.Geolocation.Geolocator();  
locator.addEventListener(  
    "positionchanged",   
     function (ev) {  
        console.log("Got event");  
    });  

You can also set the locator.onpositionchanged property:

locator.onpositionchanged =    
    function (ev) {  
        console.log("Got event");  
    };  

Another difference between .NET/C++ and JavaScript is the number of parameters taken by an event handler. In .NET/C++, a handler takes two: the event sender, and the event data. In JavaScript, the two are bundled as a single Event object. In the following example, the ev parameter contains both the sender of the event (the target property) and the event data properties (here, just position). The event data properties are the ones that are documented for each event.

function (ev) {  
    console.log("Sender: " + ev.target);  
    console.log("Position: " +  
        ev.position.latitude + "," +  
        ev.position.longitude);  
};  

Important

Windows Runtime features are not available for apps that run in Internet Explorer.

See Also

Using the Windows Runtime in JavaScript