简体   繁体   English

根据useragent重定向用户

[英]Redirect user based on useragent

I don't know JavaScript at all. 我一点都不懂JavaScript。 However, I've made an Android app in which I'm using a WebView to load a webpage. 但是,我制作了一个Android应用程序,其中使用WebView加载网页。 I have a Share option in the menu that allows to share the URL that's then loaded in the WebView. 我在菜单中有一个“共享”选项,允许共享随后在WebView中加载的URL。

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). 为了实现这一点,我的Android应用程序使用了自定义用户代理(在这种情况下为CustomUA)。 So, whenever my app loads this particular webpage (say page1.html), I want it to load normally inside the app. 因此,每当我的应用程序加载此特定网页(例如page1.html)时,我都希望它正常加载到应用程序内部。 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. 但是,假设该URL是共享的,那么我希望将该URL重定向为page2.html,如果使用任何浏览器对其进行了访问。 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. 基本上,我只希望我的应用程序的UA能够访问page1.html,而其他所有UA都将重定向到page2.html。

I suppose this can be done using JavaScript as I read some other solutions. 我想可以在阅读其他解决方案时使用JavaScript来完成此操作。 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? 另外,以防万一,如果我应该在白名单中包含多个用户代理(即,如果我希望CustomUA和CustomUA 2都能够访问该网页),该如何修改代码?

EDIT: 编辑:

I have found this code to be included in <head> : 我发现此代码包含在<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. 但是,我收到了Uncaught SyntaxError:意外的令牌{在控制台中错误,并且重定向无法正常工作。

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. 我尝试了多个表达式,例如< 0> 0== -1等。 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. 遵循您最喜欢的关于数组,循环等的JavaScript教程。

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. 这些代码中的任何一个都放在HTML页面的<head>中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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