2014-02-25 37 views
1

URL例子:jQuery的 - 獲取URL路徑的中間部分

http://www.demotest.com/categoey/t23 
http://demotest.com/categoey/t23 
http://www.demotest.net/categoey/c24 

我只(使用jQuery和JavaScript)

想從上面的URL 「demotest」 部分使用window.location.hostname我得到了完整路徑www.demotest.com但我只想demotest部分

回答

0

嘗試採用分體式,

urlParts = window.location.hostname.split('.'); 
urlParts[urlParts.length-1] 
0

拆分它通過嘗試window.location.hostname.split('.')[0]

更多 「」像這樣:

var str = "www.demotest.com" 
var arrStr = str.split("."); 

那麼你一定要會在索引1什麼 - arrStr[1]

這裏的jsfiddle - http://jsfiddle.net/N0ir/yLtCW/