Reader Level:
ARTICLE

Launching a URI in Windows Store Apps

Posted by Deepak Arora Articles | Windows Store Apps November 22, 2012
In this app I described launching a URI in a Windows Store app using JavaScript.
  • 0
  • 0
  • 3039

Introduction

In this article I describe how to create a Windows Store App for launching a URI using JavaScript. The default handler application for http is launched and displays in the foreground. This app is moved to the background. If there is no default handler for http or the "Launch Open With" button is clicked then an "Open With" dialog is displayed.

I assume you can create a simple Windows Store App using JavaScript. For more help visit Simple Windows Store Apps using JavaScript.

To start the creation of the app, add one JavaScript page by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then provide an appropriate name. In the same way, add one HTML page to your project.

launching-a-uri-in-windows St0re Apps.jpg 

Write the following code in default.html:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title></title>

    <link rel="stylesheet" href="//Microsoft.WinJS.1.0/css/ui-light.css" />

    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>

    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <link rel="stylesheet" href="/css/default.css" />

    <script src="/js/script.js"></script>

    <script src="/js/default.js"></script>

</head>

<body role="application" style="background-color: lavender">

    <center><div id="rootGrid">

        <div id="content">

            <h1 id="featureLabel"></h1>

            <div id="contentHost"></div>

        </div>

    </div></center>

</body>

</html>

Write the following code in default.js:
 

(function () {

    "use strict";

    var AppTitle = "";

 

    var pages = [

        { url: "page.html" }

    ];

    function activated(e) {

        var url = null;

        var arg = null; if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.file) {

            url = pages[2].url;

            arg = e.detail.files;

        } else if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.protocol) {

            url = pages[3].url;

            arg = e.detail.uri;

        } else if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {

            url = WinJS.Application.sessionState.lastUrl || pages[0].url;

        }

        if (url !== null) {

            e.setPromise(WinJS.UI.processAll().then(function () {

                return WinJS.Navigation.navigate(url, arg);

            }));

        }

    }

    WinJS.Navigation.addEventListener("navigated", function (evt) {

        var url = evt.detail.location;

        var host = document.getElementById("contentHost");

        host.winControl && host.winControl.unload && host.winControl.unload();

        WinJS.Utilities.empty(host);

        WinJS.UI.Pages.render(url, host, evt.detail.state).done(function () {

            WinJS.Application.sessionState.lastUrl = url;

        });

    });

    WinJS.Namespace.define("app", {

        AppTitle: AppTitle,

        pages: pages

    });

    WinJS.Application.addEventListener("activated", activated, false);

    WinJS.Application.start();

})(); 


Write the following code in page.html:
 

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <link rel="stylesheet" href="/css/launch-uri.css" />

    <script src="/js/script.js"></script>

</head>

<body>

    <button class="action" id="launchUriButton">Launch " Default Handler"</button>

    <button class="action" id="launchUriWithWarningButton">Launch "with Warning"</button>

    <button class="action" id="launchUriOpenWithButton">Launch "Open With"</button>

    <div data-win-control="app.pageOutput">

    </div>

</body>

</html>

Write the following code in script.js:
 

(function () {

    "use strict";

    var page = WinJS.UI.Pages.define("page.html", {

        ready: function (element, options) {

            document.getElementById("launchUriButton").addEventListener("click", launchUri, false);

            document.getElementById("launchUriWithWarningButton").addEventListener("click", launchUriWithWarning, false);

            document.getElementById("launchUriOpenWithButton").addEventListener("click", launchUriOpenWith, false);

        }

    });

    var uriToLaunch = "http://www.c-sharpcorner.com";

    function launchUri() {

        var uri = new Windows.Foundation.Uri(uriToLaunch);

        Windows.System.Launcher.launchUriAsync(uri).done(

                function (success) {

                    if (success) {

                        WinJS.log && WinJS.log("URI " + uri.absoluteUri + " launched.", "App", "status");

                    } else {

 

                        WinJS.log && WinJS.log("URI launch failed.", "App", "error");

                    }

                });

    }

    function launchUriWithWarning() {

        var uri = new Windows.Foundation.Uri(uriToLaunch);

        var options = new Windows.System.LauncherOptions();

        options.treatAsUntrusted = true;

        Windows.System.Launcher.launchUriAsync(uri, options).done(

                    function (success) {

                        if (success) {

                            WinJS.log && WinJS.log("URI " + uri.absoluteUri + " launched.", "App", "status");

                        } else {

 

                            WinJS.log && WinJS.log("URI launch failed.", "App", "error");

                        }

                    });

    }

    function launchUriOpenWith() {

        var uri = new Windows.Foundation.Uri(uriToLaunch);

        var options = new Windows.System.LauncherOptions();

        options.displayApplicationPicker = true;

        options.ui.selectionRect = getSelectionRect(document.getElementById("launchUriOpenWithButton"));

        options.ui.preferredPlacement = Windows.UI.Popups.Placement.below;

        Windows.System.Launcher.launchUriAsync(uri, options).done(

                   function (success) {

                       if (success) {

                           WinJS.log && WinJS.log("URI " + uri.absoluteUri + " launched.", "App", "status");

                       } else {

 

                           WinJS.log && WinJS.log("URI launch failed.", "App", "error");

                       }

                   });

    }

    function getSelectionRect(element) {

        var selectionRect = element.getBoundingClientRect();

 

        var rect = {

            x: getClientCoordinates(selectionRect.left),

            y: getClientCoordinates(selectionRect.top),

            width: getClientCoordinates(selectionRect.width),

            height: getClientCoordinates(selectionRect.height)

        };

 

        return rect;

    }

    function getClientCoordinates(cssUnits) {

        return cssUnits * (96 / window.screen.deviceXDPI);

    }

})(); 


Output:

launching-a-uri-in-windows St0re App.jpg

Summary

In this app I described launching a URI in a Windows Store app using JavaScript. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.

COMMENT USING

Trending up