2013-03-14 135 views
0

我的網址爲:/#profile/7 如何讓它從那裏抓取最後一個數字? 我正在尋找從該創建一個MySQL連接,選擇某處從某處WHERE id =':profile' 此外,是否這樣安全,它甚至可以建立連接? 這就是我所擁有的。從網址抓取哈希鏈接

<script> 
     var hash = window.location.hash.substring(6); 
     alert (hash); 
</script> 
+0

哈希不會被髮送到默認服務器。請參閱:http://stackoverflow.com/questions/317760/how-to-get-url-hash-from-server-side – Gael 2013-03-14 13:34:18

回答

0

嘗試用split()而不是子:

var parts = window.location.hash.split('/'); 
var id = parts[parts.length - 1]; 
+0

但你能回答更多的問題嗎? 使用這種方法安全嗎? – user2128056 2013-03-14 15:13:26

+0

是的,這比子字符串方法更安全,因爲這不會對散列中id之前的長度施加任何限制。 – techfoobar 2013-03-14 15:21:55

1

您可以使用正則表達式:

var hash = window.location.hash.match(/\/(\d)/)[1]