2014-02-24 27 views
0

我正在ASP.NET ASP.NET Razor v2網站,現在我有一個鏈接,可以幫助我發送用戶到一個隨機的URL,我有它作爲一個列表,它看起來喜歡這個;頁面加載/點擊後的隨機redirrect

@{ 
    List<string> UrlList = new List<string>(); 
UrlList.Add("http://example1.com/"); 
UrlList.Add("http://example2.com/"); 
UrlList.Add("http://example3.com/"); 
    Random r = new Random(); 
    int index = r.Next(UrlList.Count); 
    string randomUrl = UrlList[index]; 
} 

<a href="@randomUrl">A Random Link</a> 

當我將鼠標懸停在「A隨機鏈接」我看到在列表中的環節之一。

有沒有辦法在頁面加載後將用戶重定向到URL?

所以說簡單; 單擊「隨機鏈接」 - >加載另一個頁面 - >重新鏈接到列表中的鏈接。

+0

你想讓用戶重定向到頁面,即使沒有點擊鏈接? – Shyju

+0

不,我希望用戶在點擊鏈接後重定向。 –

+0

它應該被重定向,因爲它是一個錨標籤。你在尋找什麼行爲? – Shyju

回答

0

看起來你需要重新加載目標URL當用戶點擊this.So你應該

<a id="aRand" target="_blank" href="http://www.google.com">A Random Link</a> 

現在在你的JavaScript,當用戶點擊該鏈接,獲得隨機數和讀取的鏈接js數組並更新單擊的錨點標籤href屬性。

$(function(){ 
    var itemArr=["http://a.com","http://b.com","http://c.com"]; 

    $("#aRand").click(function(e){ 

    var randomNum=getRandom(0,itemArr.length); 
    var targetUrl=itemArr[randomNum]; 
    $(this).attr("href",targetUrl); 

    }); 

}); 

function getRandom(min, max) { 
    return Math.floor(Math.random() * (max - min) + min); 
} 

代替硬編碼的數組,你可以使用一個Ajax請求你的服務器的操作方法得到它。