[英]JavaScript redirect based on domain name
我不是在寻找简单的重定向。
我想做的就是这个。
人物A加载网站BOB.com,然后单击指向页面X的链接。
人物B加载站点TIM.com,然后单击指向同一页面X的链接。
X页面上有一个javascript命令,上面写着:如果用户来自站点Bob.com,则重定向到Bob.com/hello。
如果用户来自TIM.com,则重定向到Tim.com/hello。
如果用户不是来自以太坊,则重定向到Frank.com/opps。
该页面X将处理多个域的404错误,因此仅需查看域名最多为“ .com”的域名即可。 它应该忽略“ .com”之后的所有内容。
这是我开始使用的脚本。
<script type='text/javascript'>
var d = new String(window.location.host);
var p = new String(window.location.pathname);
var u = "http://" + d + p;
if ((u.indexOf("bob.com") == -1) && (u.indexOf("tim.com") == -1))
{
u = u.replace(location.host,"bob.com/hello");
window.location = u;
}
</script>
if(/http:\/\/(www\.)?bob\.com/.test(document.referrer)) {
window.location = "http://bob.com/hello";
}
else if(/http:\/\/(www\.)?tim\.com/.test(document.referrer)) {
window.location = "http://tim.com/hello";
}
else {
window.location = "http://frank.com/oops";
}
您可以像最初一样使用indexOf
代替正则表达式,但这也可以匹配thisisthewrongbob.com
和thisisthewrongtim.com
; 正则表达式更强大。
document.referrer
是要去的地方
使用document.referrer
查找用户来自何处。
更新的代码是
<script type='text/javascript'>
var ref = document.referrer,
host = ref.split('/')[2],
regexp = /(www\.)?(bob|tim).com$/,
match = host.match(regexp);
if(ref && !regexp.test(location.host)) {
/* Redirect only if the user landed on this page clicking on a link and
if the user is not visiting from bob.com/tim.com */
if (match) {
ref = ref.replace("http://" + match.shift() +"/hello");
} else {
ref = 'http://frank.com/oops';
}
window.location = ref;
}
</script>
工作示例 (显示消息而不是重定向)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.