[英]Get the relative path of the URL with all the parameters in Javascript?
[英]Get relative path of the page url using javascript
在javascript中,如何獲取當前網址的相對路徑?
例如http://www.example.com/test/this?page=2
我想要/test/this?page=2
嘗試
window.location.pathname+window.location.search
location.href
保存腳本運行的頁面的URL。
最快捷,最完整的方式:
location.href.replace(/(.+\w\/)(.+)/,"/$2");
您可以使用以下代碼段獲取任何頁面的絕對網址。
var getAbsoluteUrl = (function() {
var a;
return function(url) {
if(!a) a = document.createElement('a');
a.href = url;
return a.href;
}
})();
// Sample Result based on the input.
getAbsoluteUrl('/'); //Returns http://stackoverflow.com/
Checkout 使用Javascript獲取絕對URL以獲取更多詳細信息以及實現相同功能的多種方法。
location.href.replace(location.origin,'');
只有奇怪的情況: http://foo.com/ >> "/"
: http://foo.com/ >> "/"
我用這個:
var absURL = document.URL;
alert(absURL);
您應該以javascript方式使用它,從頁面中檢索包括擴展名在內的完整路徑,
$(location).attr('href');
因此,也可以檢索這樣的路徑。
www.google.com/results#tab=2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.