2016-12-18 180 views
0

我想知道,如果有人可以帮助我,我有以下的在我的网站的JavaScript代码:document.referrer帮助的Javascript

<body onload="if (document.referrer == '') self.location='https://www.thisisanexampleofmysiteurl.com';"> 

但如果有那么它包含了一个修改它的方式我想知道规则的例外。例如,如果用户直接在url中键入,他们将被发送到https://www.thisisanexampleofmysiteurl.com,但是如果他们是从URL缩写器(称为fakeexample.com)引用,那么实际的网页将加载而不是https://www.thisisanexampleofmysiteurl.com

希望这是有道理的有人可以提供帮助。

感谢,

小号

+2

重定向不会设置'document.referrer',所以没有办法告诉你通过来自缩写器的重定向来到页面。 – Barmar

回答

1

您可以添加到这段代码的else if通过一个RegExp检测是否引荐是缩短网址:

var regExp = /http:\/\/(bit.ly|goo.gl)/g; 
var dr = document.referrer; 
if (dr == '') { 

    self.location='https://www.thisisanexampleofmysiteurl.com'; 

} else if (dr.match(regExp)) { 

    self.location='http://www.whateverURLyouWantToLoadInstead.com'; 

} 

我想这可能是这是一个好主意,因为它将更清晰地包含在$(document).ready(function(){...});中。

+0

感谢您的快速响应,只是一个快速补充。如果ULR缩短器是https://anon.to,它只是一个改变bit.ly |的情况goo.gl到https://anon.to? – sc131

+0

确切地说,它会像'/ http:\/\/anon.to/g' – sebasaenz

+0

我以为这就是你想要做的。 – sebasaenz