I don’t know JavaScript at all. However, I’ve made an Android app in which I’m using a WebView to load a webpage. I have a Share option in the menu that allows to share the URL that’s then loaded in the WebView.
For a particular page, I would like the users who are not using the app to be redirected to a different page. To make this possible, my Android app is using a custom user agent (say CustomUA, in this case). So, whenever my app loads this particular webpage (say page1.html), I want it to load normally inside the app. However, suppose the URL is shared, I would like the URL to be redirected to say page2.html if it’s visited using any browser. Basically, I want only the UA of my app to be able to access page1.html and every other UA is to be redirected to page2.html.
I suppose this can be done using JavaScript as I read some other solutions. I couldn’t implement those as they were not exactly my case and I’m not sure how to implement them. I suppose, I just have to place that script in my page’s body?
Also, just in case, if I’m supposed to include multiple user agents in the whitelist (i.e., if I want CustomUA and CustomUA 2, both to be able to access the webpage), how do I modify the code?
After a lot of research, I found this code:
if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0))
{
window.location.replace("your page");
}
else
{
window.location.replace("your page");
}
</script>
and I edited it to fit my needs like this:
<script type="text/javascript">
if (navigator.userAgent.indexOf('customUA') == -1)
{
window.location.replace("page2.html");
}
</script>
however, this is still not working the way it’s expected. I tried < 0
, > 0
, <= 0
,etc., just in case they might work. But, they didn’t.
Please help me with this.