2011-11-10 73 views
12

的第一部分,如果我有一個網址,如:得到一個URL路徑

http://localhost:53830/Organisations/1216/View

我想提醒如以小寫格式的URL路徑的第一部分'組織'

到目前爲止,我有:

var first = $(location).attr('pathname'); 

first.indexOf(1); 

first.replace('/', ''); 

first.toLowerCase(); 

alert(first); 

但預期它不工作。誰能幫忙?由於

+2

@YuriyFaktorovich不在英國,不是;) – Curt

+0

.indexOf有什麼用? – Eduardo

+0

那是因爲你英國人不會說適當的英語;或者也許我們美國人只是認爲我們知道一切;)Dogbert的回答看起來很穩固,btw – Kato

回答

13
var first = $(location).attr('pathname'); 

first.indexOf(1); 

first.toLowerCase(); 

first = first.split("/")[1]; 

alert(first); 
1

嘗試使用first.split('/')所以你最終會用繩子像

['http:' ,'', 'localhost:53830' , 'Organisations' , '1216' , 'View' ] 

一個數組,然後找到一個爲localhost之後:53830

17
location.pathname.split('/')[1] 

我做但是看不到需要.toLowerCase()

+3

只需注意您不需要jQuery。平原的Javascript將做的伎倆: 'location.pathname.split('/')[1]' –

相關問題