Javascript 基于useragent的重定向用户

Javascript 基于useragent的重定向用户,javascript,Javascript,我一点都不懂JavaScript。然而,我已经制作了一个Android应用程序,在其中我使用WebView加载网页。我在菜单中有一个共享选项,允许共享随后加载到WebView中的URL 对于特定页面,我希望未使用该应用程序的用户被重定向到其他页面。为了实现这一点,我的Android应用程序使用了一个自定义用户代理(在本例中是CustomUA)。所以,每当我的应用程序加载这个特定的网页(比如page1.html),我都希望它在应用程序中正常加载。但是,假设URL是共享的,如果使用任何浏览器访问,我

我一点都不懂JavaScript。然而,我已经制作了一个Android应用程序,在其中我使用WebView加载网页。我在菜单中有一个共享选项,允许共享随后加载到WebView中的URL

对于特定页面,我希望未使用该应用程序的用户被重定向到其他页面。为了实现这一点,我的Android应用程序使用了一个自定义用户代理(在本例中是CustomUA)。所以,每当我的应用程序加载这个特定的网页(比如page1.html),我都希望它在应用程序中正常加载。但是,假设URL是共享的,如果使用任何浏览器访问,我希望URL重定向为page2.html。基本上,我只希望我的应用程序的UA能够访问page1.html,并且每隔一个UA将被重定向到page2.html

我想这可以在阅读其他解决方案时使用JavaScript来完成。我无法实现这些,因为它们不完全是我的情况,我不知道如何实现它们。我想,我只需要把脚本放在我的页面主体中

另外,以防万一,如果我应该在白名单中包括多个用户代理(即,如果我希望CustomUA和CustomUA 2都能够访问该网页),我如何修改代码

编辑:

我发现此代码包含在
中:


if((navigator.userAgent.indexOf('MSIE')>=0)和&(navigator.userAgent.indexOf('Opera')<0))
{
window.location.replace(“您的页面”);
}
其他的
{
window.location.replace(“您的页面”);
}
现在,有人能帮我根据我的需要修改它吗?我只是希望网页被重定向,如果它不是我的自定义用户代理,并留在同一页,如果它是

编辑:

我使用的代码如下:

<script>

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

</script>

if((navigator.userAgent.indexOf('customUA')<0)
{
window.location.replace(“page2.html”);
}
然而,我得到了未捕获的SyntaxError:控制台中意外的令牌{eror,重定向不起作用

编辑:

已绕过未捕获错误。新代码:

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

if(navigator.userAgent.indexOf('customUA')>=0)
{
window.location.replace(“page2.html”);
}

现在,它在任何一种情况下都可以正常工作,也就是说,它将重定向到我不希望它重定向的地方,反之亦然。我尝试了多种表达式,如
<0
<0
==-1
,等等。没有一种工作正常。基本上,它没有给出预期的结果。我很快就能找到我的答案。请帮帮我!

首先,除非有很好的技术理由想这样做,不要这样做。这是互联网;让人们随意访问你的页面

其次,换一种方式可能更容易做到这一点——在web上有一组普通页面,但如果在应用程序中访问这些页面,应用程序会检测到它们,并导航到特定于应用程序的版本


最后,如果您这样做,为了方便处理多个用户代理,您需要创建一个包含所有用户代理字符串的数组,并循环检查每个字符串。请遵循您最喜欢的有关数组、循环等的JavaScript教程。

一些论坛的人帮助了我,而且,我设置了错误的用户代理(我在我的应用程序中定义的与我在此脚本中键入的不同)。因此,代码如下:

<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>

var uaCheck=navigator.userAgent.indexOf('customuseragent');
如果(uaCheck==-1)
{
window.location.replace(“https://www.domain.tld/redirected-page.html");
}
如果有多个用户代理,则代码如下:

<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>

函数isMyAppUA(testUA)
{
if(testUA==未定义)返回false;
var appUAList=
[
“customUA1”、“customUA2”、“customUA3”、“customUA3”、“customUAn”
];
appalist.forEach((ua)=>
{
如果(测试ua.indexOf(ua)>-1)
{
返回true;
}
});
返回false;
}
如果(!isMyAppUA(navigator.userAgent))
{
window.location.replace(“https://www.domain.tld/redirected-page.html");
}
如果用户代理列表较小,则此代码的版本较短:

<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>

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

>这些代码中的任何一个都在HTML页面的<代码> <代码>中。

有一些页面不应该显示给最终用户。考虑一下这一点,就这样。关于第二点,是的。它有点意义。现在,我想重定向主页到应用程序页面,如果页面是用我的应用程序访问的。第三件事,我一定会。好的,关于它的更多信息-谢谢你的提示。这很奇怪,因为>=0应该是正确的条件。你是否尝试过使用警报来显示你正在检测的用户代理?警报(navigator.userAgent);
<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>