2014-02-25 39 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/