2011-09-15 104 views
0

我有幾個網址,我需要允許訪問前頁面加載和其餘的URL將被重定向。允許網址進入網站嗎?

expample: If user trying to access these urls they will be allowed and 
    other urls will be redirected to google.com (example) 

http://mysite.com/site/Dept/IT 
http://mysite.com/site/Dept/IT/ITWS 
http://mysite.com/site/dept/ce 
http://mysite.com/site/dept/mkt 
http://mysite.com/site/teams/dgt 
http://mysite.com/site/teams/mm 

在jQuery中如何做到這一點?爲URL製作數組列表並檢查數組列表並允許它們。

+0

如果您不反對將PHP用於您的網頁,這在PHP中很容易實現。 – Wex

+0

如果關閉了JavaScript,該怎麼辦? –

+0

@Wex:這是asp.net masterpage。 – James123

回答

1

Use this for external URL's.

$(document).ready(function() { 
    $('a').click(function() { 
     if (!$(this).attr("href").match("^http://yoursite.com.*$")) 
     { 
      if (!confirm("Do you realy want to leave this site?")) 
      { 
       return false; 
      } 
     } 
    }); 
}); 

如果你真的想拒絕特殊的URL是你的服務器上我真的建議你做服務器端。

2

您可以在每個頁面上加載JavaScript代碼段,並且如果當前頁面的url不匹配任何這些頁面,它將重定向用戶。

$(document).ready(function() { 
    var urls = ["http://mysite.com/site/Dept/IT", 
       "http://mysite.com/site/Dept/IT/ITWS", 
       "http://mysite.com/site/dept/ce", 
       "http://mysite.com/site/dept/mkt", 
       "http://mysite.com/site/teams/dgt", 
       "http://mysite.com/site/teams/mm"]; 
    if ($.inArray(document.location.href, urls) == -1) { 
     window.location = "http://www.google.com/"; 
    } 
});