Freigeben über


JavaScript for Windows 8 Apps: How to Access the User’s Display Name

Need to know the display name of the user currently logged in to Windows 8?  The task is easy!

To demonstrate, I created a new “Blank App” JavaScript project in Visual Studio 2012.  In the default.html file, I replaced the contents of the <body> tag with the following:

 <body>
    <h1 id="displayName">(to be replaced by user's display name)</h1>
    <script>
        Windows.System.UserProfile.UserInformation.getDisplayNameAsync().then(
            function (name) {
                document.querySelector("#displayName").textContent = name;
            }
        );
    </script>
</body>

When I run the application, my <h1> contents contain my display name.

SNAGHTML6b4d0e

Of course the JavaScript code above could be used in an external .js file as well.