是否有可能使用javascript獲取當前頁面?如何使用javascript獲取當前頁面
例如,如果我到達子頁面的主頁,我將字符串名稱index.php
或如果我去關於頁面我可以得到about.php
。
我試試這個代碼: window.location.pathname
,但我在我的子域回報得到這個。 /directory/new/index.php
和/directory/new/about.php
有沒有人有關於我的情況的想法?任何幫助將不勝感激。
是否有可能使用javascript獲取當前頁面?如何使用javascript獲取當前頁面
例如,如果我到達子頁面的主頁,我將字符串名稱index.php
或如果我去關於頁面我可以得到about.php
。
我試試這個代碼: window.location.pathname
,但我在我的子域回報得到這個。 /directory/new/index.php
和/directory/new/about.php
有沒有人有關於我的情況的想法?任何幫助將不勝感激。
window.location.href
或top.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
試試這個
window.location.href
它們不會吐出當前的文件名,只有完整的URL – Thielicious
我不認爲你可以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:"
window.location.pathname.split("/").pop();
爲/directory/new/index.php,分割( 「/」)將返回[, 「目錄」,「新「,」index.php「],pop()將返回最後一個元素index.php。
簡單的'document.URL.match(/ [^ \ /] + $ /);' – Thielicious