Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether a website runs in Android browser or webView of an app

Tags:

javascript

Update: thanks for the answers, but I want to clarify that I am a javascript developer and not an android app developer, and I need to detect on runtime whether a website is loaded in Android's browser or loaded in a WebView of an app, only by javascript code

Original Question: How can Javascript detect on runtime whether a website is loaded in Android's browser or loaded in a WebView of an app, when I am not in charge on the application?

like image 559
Natalie Gabel Avatar asked Oct 20 '22 19:10

Natalie Gabel


1 Answers

I believe that the user agent will be the same for both the browser and the webview so that won't tell the difference.

What you can do however is to attempt to bind javascript code to android code and call it. If it succeeds then its running in webview, or if it fails then its in browser.

For more information on how to do this see:

http://developer.android.com/guide/webapps/webview.html#BindingJavaScript

Example (Java/Javascript may not be right):

@JavascriptInterface
public void userAgentDetect() {
    WebView = true;
}

Set the WebView variable to false then attempt to run the function.

Then check that WebView boolean to confirm whether its in the app or browser.

EDIT: Here is another example: Detect inside Android Browser or WebView

like image 158
OneRainbow Avatar answered Oct 22 '22 08:10

OneRainbow