简体   繁体   中英

Redirect user based on useragent

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 (ie, if I want CustomUA and CustomUA 2, both to be able to access the webpage), how do I modify the code?

EDIT:

I have found this code to be included in <head> :

<script>

if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0))
{
    window.location.replace("your page");
}
else 
{
   window.location.replace("your page");
}

</script>

Now, can someone lease help me to modify it according to my needs? I just want the webpage to be redirected if it's not my custom User Agent and to stay on the same page if it is.

EDIT:

I have used the code like this:

<script>

if ((navigator.userAgent.indexOf('customUA') < 0)
{
window.location.replace("page2.html");
}

</script>

however, I'm getting Uncaught SyntaxError: Unexpected token { eror in console and the redirect isn't working.

EDIT:

Got around the Uncaught error. New code:

<script type="text/javascript">
if (navigator.userAgent.indexOf('customUA') >= 0)
{
window.location.replace("page2.html");
}
</script>

Now, it's working fine in either of the one ie, it'll redirect where I don't want it to and vice versa. I've tried multiple expressions like < 0 , > 0 , == -1 , etc. None worked fine. Basically, it's not giving the expected results. I'm this close to find my answer.. please, help!

First off, unless there is good technical reason to want to do this, don't. This is the internet; let people access your pages however they wish.

Second, it might be easier to do this the other way around- have a set of normal pages on the web, but if they are accessed in the app, the app detects them, and navigates to an app-specific version.

Finally, if you do do this, for easily handling multiple user agents, you would want to make an array containing all the user agent strings you want, and loop through them to check each one. Follow your favorite JavaScript tutorial on arrays, loops, etc.

Some guys from a forum helped me, also, I was setting the wrong user agent (the one that I had defined in my app was different than what I had typed in this script). So, here goes the code:

<script type="text/javascript">
var uaCheck = navigator.userAgent.indexOf('Custom User Agent');
if (uaCheck == -1)
{
window.location.replace("https://www.domain.tld/redirected-page.html");
}
</script>

And this is the code if there are multiple user agents:

<script type="text/javascript">

function isMyAppUA(testUA)
{
    if(testUA == undefined) return false;

    var appUAList = 
    [
        'customUA1', 'customUA2', 'customUA3', ..., 'customUAn'
    ];

    appUAList.forEach((ua) => 
    {
        if(testUA.indexOf(ua) > -1) 
        {
            return true;
        }
    });
    return false;
}

if(!isMyAppUA(navigator.userAgent)) 
{
    window.location.replace("https://www.domain.tld/redirected-page.html");
}

</script>

There's a shorter version of this code if the list of user agents is small:

<script type="text/javascript">

if (navigator.userAgent.indexOf('customUA') == -1 || navigator.userAgent.indexOf('customUA2') == -1) 
{
    window.location.replace("https://www.domain.tld/redirected-page.html");
}

</script>

Any of these codes go in the <head> of the HTML page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM