2012-12-03 44 views
0

是否有可能使用javascript獲取當前頁面?如何使用javascript獲取當前頁面

例如,如果我到達子頁面的主頁,我將字符串名稱index.php或如果我去關於頁面我可以得到about.php

我試試這個代碼: window.location.pathname

,但我在我的子域回報得到這個。 /directory/new/index.php/directory/new/about.php

有沒有人有關於我的情況的想法?任何幫助將不勝感激。

+0

簡單的'document.URL.match(/ [^ \ /] + $ /);' – Thielicious

回答

1

window.location.hreftop.location.href 您將獲得「http://stackoverflow.com/questions/ 13678553/how-to-get-the-current-page-using-javascript「。

 
`var url=window.location.href.split('/'); 
var name=url[url.length-1];` 

名稱是在index.php或about.php

0

試試這個

window.location.href 
0

我不認爲你可以directl Ÿ得到你想要的,嘗試這樣的:

var segments = window.location.pathname.split('/'); 
var result = segments[segments.length - 1]; 

以下是有關當前問題的URL數據:

hash: "#13678603" 
host: "stackoverflow.com" 
hostname: "stackoverflow.com" 
href: "http://stackoverflow.com/questions/13678553/how-to-get-the-current-page-using-javascript/13678603#13678603" 
origin: "http://stackoverflow.com" 
pathname: "https://stackoverflow.com/questions/13678553/how-to-get-the-current-page-using-javascript/13678603" 
port: "" 
protocol: "http:" 
1
window.location.pathname.split("/").pop(); 

爲/directory/new/index.php,分割( 「/」)將返回[, 「目錄」,「新「,」index.php「],pop()將返回最後一個元素index.php。