locked
how to check if my JavaScript app runs in metro mode

    Question

  • I'm porting a web app to metro, but want to maintain one source code base. I want to enable some metro specific functions when the app runs in metro mode. how do I detect if the app is running in metro mode in the JavaScript?

    thanks,

    Jennifer

    Monday, October 08, 2012 10:06 PM

Answers

  • To test whether a variable is defined at all, use if (typeof <var> === "undefined") instead of testing the value. That is, if (Windows) is trying to evaluate the value of an undefined variable, so it throws an exception. typeof, on the other hand, will return "undefined" (the string) if the variable doesn't exist in the current scope.

    So this works in IE:

    if (typeof Windows === "undefined") {
        alert("as web app");
    } else {
        console.log("as Windows Store app");
    }
    

    Note that window.alert is not available for Win8 Store apps, so I've changed that output to something that does.

    • Marked as answer by jennj Tuesday, October 09, 2012 5:55 PM
    Tuesday, October 09, 2012 5:21 PM

All replies

  • You could check that the Windows namespace is defined, as it will be in an app but not on the web. You'd have to do that if you were going to employ any WinRT APIs in the app code anyway.

    Tuesday, October 09, 2012 12:29 AM
  • thanks for your response.

    I tried it as below, it works only in Metro, but not in IE10. IE10 stops when seeing "Windows" as undefined. 

    if (Windows) 
        alert ("as metro app");
    else 
        alert ("as web app");

    What I did is: 

    var isMetroApp=false;

    // then set isMetroApp=true in "default.js" in following code: 

       app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize
                    // your application here.
                    isWinJSApp = true;
                } else {
                    // TODO: This application has been reactivated from suspension.
                    // Restore application state here.
                }
                args.setPromise(WinJS.UI.processAll());
            }
        };
    

    Then "isWinJSApp" won't cause any issue with IE10. 

    Would prefer a simpler method though. 

    Jennifer 

    Tuesday, October 09, 2012 4:44 AM
  • To test whether a variable is defined at all, use if (typeof <var> === "undefined") instead of testing the value. That is, if (Windows) is trying to evaluate the value of an undefined variable, so it throws an exception. typeof, on the other hand, will return "undefined" (the string) if the variable doesn't exist in the current scope.

    So this works in IE:

    if (typeof Windows === "undefined") {
        alert("as web app");
    } else {
        console.log("as Windows Store app");
    }
    

    Note that window.alert is not available for Win8 Store apps, so I've changed that output to something that does.

    • Marked as answer by jennj Tuesday, October 09, 2012 5:55 PM
    Tuesday, October 09, 2012 5:21 PM
  • I also wanted to know windows 8 mode using java script in IE 10

    Please help me if you have done it before


    Chris V R

    Monday, November 26, 2012 11:08 AM