简体   繁体   中英

Conditional statement to check for Win8 or iOS

I'm writing an HTML5 / JavaScript app for multiple devices, but my issue comes when writing it for Win8 and iOS.

Due to Win8's strict security, I need to wrap certain functions, specifically those that inject HTML or divs into the window, in a WinJS function called execUnsafeLocalFunction.

When I go to run this same app on iOS, it cannot run those functions because WinJS does not exist.

How can I create a conditional statement so that it checks if the device running the app is a Win8 device, or iOS? This way I can tell it to run function foo or function bar .

JavaScript doesn't presently support conditional compilation, so I think you need to write your own stub functions that wrap those like execUnsafeLocalFunction. In that function you'd test whether the function exists by checking the namespaces. Something like:

function callUnsafeFunction(func) {
    if (MSApp && MSApp.execUnsafeLocalFunction) {
        return MSApp.execUnsafeLocalFunction(func);
    } else {
        return func() 
    }
}

Alternately, you can check if the function exists in the namespace, and if not, add you own function of the same name as a passthrough, something like this (I haven't tested such code):

if (!(MSApp && MSApp.execUnsafeLocalFUnction)) {
    window.MSApp = {
        execUnsafeLocalFunction: function(func) {
            func();
        }
    };
}

In short, the conditional check is the existence of a namespace/function, because we don't have conditional compilation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM