2015-02-24 51 views
2

我正在爲我的網站使用JavaScript「倒計時然後重定向」腳本。我想使用類似HTTP引用在下面的腳本附:如何有條件地將引用鏈接重定向到不同的網頁?

<script> 
<!-- 

/* 
Count down then redirect script 
By JavaScript Kit (http://javascriptkit.com) 
*/ 

//change below target URL to your own 
var targetURL="www.domain.com"; 

//change the second to start counting down from 
var countdownfrom=20; 

var currentsecond = document.redirect.redirect2.value = 
    countdownfrom + 1; 

function countredirect() { 
    if (currentsecond != 1) { 
    currentsecond -= 1; 
    document.redirect.redirect2.value = currentsecond 
    } else { 
    window.location = targetURL; 
    return; 
    } 

    setTimeout(countredirect, 1000); 
} 

countredirect(); 
//--> 
</script> 

我希望腳本一樣工作:

如果有人從www.twitter.com到達時,它們將被重定向到www .domain.com/twitter.html倒數後。

如果有人從www.facebook.com抵達,他們將在倒計時後重定向到www.domain.com/facebook.html。

如果有人從www.youtube.com抵達,他們將在倒計時後重定向到www.domain.com/youtube.html。

我想重定向15-20 URL。請幫忙 !!我該怎麼做 ??

+1

我已經刪除了自己[標籤:JAVA]標籤爲你的問題似乎已經無關,與使用這種語言。 – 2015-02-24 19:18:38

回答

2

簡單:

var targetURL = /www\.xxx\.com/i.test(document.referrer) ? 
    "www.domain-for-xxx.com" : "www.domain-for-yyy.com"; 

多從哪裏來:

var targetUrl = "http://defaulturl.com/"; 
if (/www\.xxx\.com/i.test(document.referrer)) { 
    targetUrl = "http://www.domain-for-xxx.com/"; 
} else if (/www\.yyy\.com/i.test(document.referrer)) { 
    targetUrl = "http://www.domain-for-yyy.com/"; 
} 
... 
+0

我需要添加多個引用者並定義他們的重定向URL ......我該如何做? – Ron 2015-02-24 19:54:47

+0

只要使用'if(xxx){targetUrl ='xxx'} else if(yyy){targetUrl ='yyy'}'block。你可以有任意多的'else if'語句。 – 2015-02-24 19:56:00

+0

如何在這裏定義多個引用者?如果我使用4/5的引用,那我應該放在這裏? var targetURL = /www..xxx\.com/i.test(document.referrer)? 「www.domain-for-xxx.com」:「www.domain-for-yyy.com」; – Ron 2015-02-24 20:27:31

相關問題