我只想获取网站URL。不是从链接获取的URL。在页面加载时,我需要能够获取网站的完整、当前URL,并将其设置为一个变量,以便根据需要进行处理。


当前回答

要获取路径,可以使用:

http://www.example.com:8082/index.php#tab2?foo=789

Property                    Result
------------------------------------------
window.location.host        www.example.com:8082
window.location.hostname    www.example.com
window.location.port        8082
window.location.protocol    http:
window.location.pathname    index.php
window.location.href        http://www.example.com:8082/index.php#tab2
window.location.hash        #tab2
window.location.search      ?foo=789
window.location.origin      https://example.com

其他回答

let url=新url(window.location.href);console.log(url.href);

使用以上代码获取网站的当前URL。

或者试试这个https://bbbootstrap.com/code/get-current-url-javascript-54628697

您可以通过location.href获取当前页面的完整链接要获取当前控制器的链接,请使用:

location.href.substring(0, location.href.lastIndexOf('/'));
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
       currentPageUrlIs = this.href.toString().toLowerCase(); 
}else{ 
       currentPageUrlIs = document.location.toString().toLowerCase();
}

以上代码也可以帮助某人

你有多种方法可以做到这一点。

1:

location.href;

2:

document.URL;

3:

document.documentURI;

短的

location+''

let url=location+'';console.log(url);