How can Javascript detect whether a website is loaded in Android's stock browser or loaded in a WebView of another app? I would like to run slightly different code in these two cases.
Asked
Active
Viewed 3.3k times
40
-
Check to see if the webview user agent is any different than the Android stock browser. You can check it fairly easily by going to this site. http://whatsmyuseragent.com/ However I am fairly certain the user agent will be exactly the same in which case I have no idea. – Caimen Jul 21 '11 at 21:46
-
Unless you are the creator of the WebView in question. In which case I think you can put in whatever you want for user agent – FoamyGuy Jul 21 '11 at 21:53
-
2If you're in charge of the application, is it possible that you could send a specific user agent when you load the page, and then use Caimen's suggestion above? Using [WebView's setUserAgentString](http://developer.android.com/reference/android/webkit/WebSettings.html#setUserAgentString(java.lang.String)) function, you could set a specific user agent, and then user javascript to detect the difference. – mopsled Jul 21 '11 at 22:02
-
1I am the author of the app which uses the WebView. I was hoping for a way to detect it without writing additional code, but this is the temporary solution: `this.webView.getSettings().setUserAgentString(this.webView.getSettings().getUserAgentString() + getString(R.string.user_agent_suffix));` where user_agent_suffix is " AppName/1.0". I hope that's compliant with the user agent string standards. – JoJo Jul 21 '11 at 22:13
3 Answers
57
Activity -> onCreate
this.webView.getSettings().setUserAgentString(
this.webView.getSettings().getUserAgentString()
+ " "
+ getString(R.string.user_agent_suffix)
);
Res -> Values -> strings.xml
<string name="user_agent_suffix">AppName/1.0</string>
Javascript
function() isNativeApp {
return /AppName\/[0-9\.]+$/.test(navigator.userAgent);
}

JoJo
- 19,587
- 34
- 106
- 162
-
4That's the official recommended way from http://developer.android.com/guide/webapps/webview.html#EnablingJavaScript – Marvin Emil Brach Jul 27 '12 at 08:49
-
@MarvinEmilBrach that does not work if you are using social media auth in the webview. It will lose the Javascript interface – user516883 Oct 30 '15 at 19:28
-
2This is viable only when you are also the Android app developer, but it won't work if you can't change the app source code in which the page will be running. Unless, of course, if you contact the app developers and they agree to make the changes in the Android code for you. – Ulysses Alves Nov 10 '15 at 10:52
-
I am getting this in webview: ``` navigator.userAgent 'Mozilla/5.0 (Linux; Android 13; SM-G781B Build/TP1A.220624.014; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/115.0.5790.166 Mobile Safari/537.36' ``` – Luckylooke Aug 16 '23 at 07:04
5
You can check the server variables on the page that is being requested to see if it is coming from your app and set a javascript variable accordingly
if($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app")
echo 'var isAndroidApp=true;';
else
echo 'var isAndroidApp=false;';
- replace com.company.app with your package name

Sean
- 2,033
- 2
- 23
- 28
3
In the newer versions of WebView, Lollipop and above, you can differentiate the WebView by looking for the wv field in user agent string:
Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36
https://developer.chrome.com/multidevice/user-agent#webview_user_agent

pera
- 882
- 11
- 21