繁体   English   中英

JS根据用户代理重定向?

[英]JS redirect according to user agent?

如果这个问题很明显可以解决,我很抱歉,但我还不知道 JavaScript 但感觉这是实现我的客户对其网站所需的唯一方法。

我想在用户访问主页后将使用特定浏览器的用户重定向到特定的简化页面,并在他们每次单击主页按钮时保持此重定向工作。 基本上我想要的是基于用户代理的 301 重定向。 我已经尝试了很多次,但我得到的只是一个无限的重定向循环或根本没有结果。

function get_ya_browser(){
var ua = navigator.userAgent;    
if (ua.search(/YaBrowser/) > 0) document.location.href='redirect-link.html';
return '';
}

我怎样才能改变这个代码工作? 非常感谢任何帮助。

尝试 - document.location.replace ='redirect-link.html'; 而不是 document.location.href ='redirect-link.html';

.location.replace() 模拟 HTTP redirect.location.href() 模拟鼠标点击

如果代码是redirect-link.html && ua.search(/YaBrowser/) !== -1的一部分,您将得到一个无限循环。

以下代码解决了这个问题:

function get_ya_browser(){
   var ua = navigator.userAgent;    
   if (ua.search(/YaBrowser/) !== -1 && !location.href.includes('elbrus-m-zakaz.ru/home-page-windows-xp/')) {
      document.location.href='//elbrus-m-zakaz.ru/home-page-windows-xp/';
   }
}

暂无
暂无

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

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