13

I've got a third party script that runs in many places on the web. I'd like to be able to tell when I'm running in an app vs. a browser. The user agent doesn't always help. Chrome provides the chrome object which has some different properties depending on the environment. Anyone know of anything similar for IE?

This is for a product similar to google analytics which can be implemented or wind up in many environments, and I'm trying to distinguish them better.

reo katoa
  • 5,751
  • 1
  • 18
  • 30
sprugman
  • 19,351
  • 35
  • 110
  • 163
  • What exactly do you need to know this for? Also, what specific "trident-based application"s do you have in mind? – Bergi Feb 26 '14 at 20:44
  • 1
    Please describe the problem you're actually trying to solve, not the solution you're trying to pursue. If we knew why you needed to know the difference, then we could better know what answers might work best. – jfriend00 Feb 26 '14 at 20:51
  • 1
    I'm trying to solve a general problem for ALL trident-based apps if possible: if my script shows up in the app, can I tell whether it's in an app or in a browser? That's the problem. It's for a product similar to google analytics which can be implemented or wind up in many environments, and I'm trying to distinguish them better. – sprugman Feb 27 '14 at 21:01
  • 1
    _“if my script shows up in the app, can I tell whether it's in an app or in a browser? That's the problem”_ – no, that’s not the _problem_ – it is the poor _solution_ you have come up with so far to fix whatever the _real_ problem is. So now please tell us what that is, finally. – CBroe Feb 27 '14 at 21:23
  • 6
    It sounds like the OP needs this ability as a data point for analytics, not as a solution to a technical challenge. – reo katoa Feb 28 '14 at 05:58
  • 2
    @WinnieNicklaus has it correct: I want to know this as a data point to correlate with other factors. – sprugman Feb 28 '14 at 22:03
  • I'm sure you thought of this, but isn't the global object fundamentally different in a browser vs some other environment? Can't you check that to determine if you're in a browser? – Adam Rackis Mar 05 '14 at 19:23
  • Try and see what the `navigator` object has to say in different environments as well. – CBroe Mar 05 '14 at 21:26
  • `window.location.hostname` may help. Ostensibly, an app doesn't sit on the web — whereas a website will. – Barney Mar 12 '14 at 18:44

2 Answers2

1

Check for window.external. If window.external is null then the web page is loaded into a Web Component, otherwise it is loaded via browser.

window.external enable you to communicate with [ComVisible(true)] instances which are enabled by default in the browsers. As long as your apps are not decorated with [ComVisible(true)], you can distinguish between app and browser page consumption.

0

The way Google Analytics checks for browser and device is the HTTP User-Agent header. It lets you check for browser, browser version, OS, OS version, and sometimes device:

http://jonathanstark.com/blog/windows-phone-8-user-agent-string

http://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx

You can parse this string in Javascript as well: Getting the User Agent with JavaScript

Community
  • 1
  • 1
Apollo Clark
  • 806
  • 9
  • 16