使用JavaScript获取当前URL?

JavaScript

樱古一

2020-03-09

我想要的只是获取网站URL。不是从链接获取的URL。在页面加载过程中,我需要能够获取网站的完整,当前URL,并将其设置为一个变量,以便我随意使用。

第160篇《使用JavaScript获取当前URL?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

18个回答
达蒙阿飞斯丁 2020.03.09

Nikhil Agrawal的答案很好,只需在此处添加一个小示例,您就可以在控制台中进行操作以查看实际的不同组件:

在此处输入图片说明

If you want the base URL without path or query parameter (for example to do AJAX requests against to work on both development/staging AND production servers), window.location.origin is best as it keeps the protocol as well as optional port (in Django development, you sometimes have a non-standard port which breaks it if you just use hostname etc.)

GilJinJin 2020.03.09

Try

location+''

Jim猴子Eva 2020.03.09

使用JavaScript获取当前URL:

  • window.location.toString();

  • window.location.href

满天星 2020.03.09

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

location.href.substring(0, location.href.lastIndexOf('/'));
卡卡西Pro小卤蛋 2020.03.09

获取当前位置对象的方法是window.location

将此与进行比较document.location,后者最初仅将当前URL作为字符串返回。可能是为了避免混淆,document.location已将替换为document.URL

并且,所有现代浏览器都映射document.locationwindow.location

实际上,为了确保跨浏览器的安全,您应该使用window.location而不是document.location

飞云前端西门 2020.03.09

在jstl中,我们可以使用来访问当前的URL路径pageContext.request.contextPath如果要进行Ajax调用,请使用以下URL。

url = "${pageContext.request.contextPath}" + "/controller/path"

示例:对于页面,http://stackoverflow.com/posts/36577223将给出http://stackoverflow.com/controller/path

理查德凯 2020.03.09
   location.origin+location.pathname+location.search+location.hash;
逆天 2020.03.09

对于那些想要实际URL对象的人,可能是将URL作为参数的实用程序:

const url = new URL(window.location.href)

https://developer.mozilla.org/zh-CN/docs/Web/API/URL

猴子阳光 2020.03.09

对于带有查询字符串的完整URL:

document.location.toString().toLowerCase();

对于主机URL:

window.location
ItachiGreen 2020.03.09
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
       currentPageUrlIs = this.href.toString().toLowerCase(); 
}else{ 
       currentPageUrlIs = document.location.toString().toLowerCase();
}

上面的代码也可以帮助某人

西门小宇宙 2020.03.09

添加结果以供快速参考

window.location;

 Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
 ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ, …}

文件位置

  Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript", 
ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ
, …}

window.location.pathname

"/questions/1034621/get-the-current-url-with-javascript"

window.location.href

"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"

location.hostname

"stackoverflow.com"
伽罗Pro卡卡西 2020.03.09

使用:window.location.href

如上所述,document.URL 更新不会更新window.location参见MDN

逆天Eva 2020.03.09

打开Developer Developer,在控制台中键入以下内容,然后按Enter

window.location

例如:以下是当前页面上结果的屏幕截图。

在此处输入图片说明

从这里获取您的需求。:)

凯斯丁 2020.03.09
  • 使用window.location.href以获得完整的URL。
  • 使用window.location.pathname得到URL离开主机。
Tom阿飞 2020.03.09

获取当前页面的URL:

window.location.href
神乐小胖 2020.03.09

用于window.location与当前帧关联位置对象的读写访问如果您只想以只读字符串的形式获取地址,则可以使用document.URL,其值应与相同window.location.href

路易斯丁神奇 2020.03.09

URL信息访问

JavaScript为您提供了许多方法来检索和更改当前URL,该URL显示在浏览器的地址栏中。所有这些方法都使用Location对象,它是对象的属性Window您可以创建一个Location具有当前URL 的新对象,如下所示:

var currentLocation = window.location;

基本网址结构

<protocol>//<hostname>:<port>/<pathname><search><hash>
  • protocol:协议名称,用于访问Internet上的资源。(HTTP(不带SSL)或HTTPS(带SSL))

  • hostname:主机名指定拥有资源的主机。例如,www.stackoverflow.com服务器使用主机名提供服务。

  • 端口:端口号,用于识别Internet或其他网络消息到达服务器时将转发到的特定进程。

  • pathname:路径提供有关Web客户端要访问的主机内特定资源的信息。例如,/index.html

  • 查询:查询字符串位于路径部分之后,并提供资源可用于某些目的的信息字符串(例如,用作搜索的参数或要处理的数据)。

  • hash: URL的锚点部分,包括井号(#)。

使用这些Location对象属性,您可以访问所有这些URL组件以及它们可以设置或返回的内容:

  • href-整个网址
  • protocol -URL的协议
  • host -URL的主机名和端口
  • hostname -URL的主机名
  • port-服务器用于URL的端口号
  • pathname -URL的路径名
  • search -URL的查询部分
  • hash -URL的锚点部分

我希望你能得到答案。

JinJinPro 2020.03.09

采用:

window.location.href 

如评论中所述,下面的行有效,但对于Firefox而言是错误的。

document.URL;

请参阅DOMString类型的URL,只读

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android