I’m my previous post I showed how you can add a website to a universal project and share some JavaScript between all three. This is nice, but I will run into a situation where you’ll have some code that is not supposed to run on one of the platforms. In the C# world you can use compiler directives in situations like this. In JavaScript this is not possible (Although you could create constructions that mimic directives using Grunt for example). WinJS has some functionality build in that can help finding out what platform you are using.
There are two properties on the utilities in WinJS you’ll need:
- WinJS.Utilities.isPhone : will return true if the app is running on the phone
- WinJS.Utilities.hasWinRT : will return true if WinRT is available in the context you are running
Here’s a small example of you could use it:
if (WinJS.Utilities.isPhone) { console.log("Phone"); } else { if (WinJS.Utilities.hasWinRT) { console.log("WinApp"); } else { console.log("Else"); } }