乐闻世界logo
搜索文章和话题

JS 如何获取当前 url

5个答案

1
2
3
4
5

要使用JavaScript获取当前页面的URL,您可以使用 window.location 对象,它包含了与当前窗口的位置相关的信息。下面是获取当前URL的一些属性和方法:

  1. window.location.href :返回完整的URL字符串(即它包含协议、主机、端口号(如果有)、路径、查询字符串、锚点等)。

例如,假设当前URL是 "https://www.example.com:80/path/index.html?search=test#section1",您可以这样获取完整的URL:

javascript
var currentUrl = window.location.href; console.log(currentUrl); // 输出:"https://www.example.com:80/path/index.html?search=test#section1"
  1. window.location.hostname :返回web服务器的域名。

例如:

javascript
var hostname = window.location.hostname; console.log(hostname); // 输出:"www.example.com"
  1. window.location.pathname:返回URL的路径部分。

例如:

javascript
var pathname = window.location.pathname; console.log(pathname); // 输出:"/path/index.html"
  1. window.location.protocol:返回页面使用的协议,通常是http:或https:。

例如:

javascript
var protocol = window.location.protocol; console.log(protocol); // 输出:"https:"
  1. window.location.port:返回服务器端口号。

例如:

javascript
var port = window.location.port; console.log(port); // 输出:"80"
  1. window.location.search:返回URL的查询字符串部分,以"?"开头。

例如:

javascript
var search = window.location.search; console.log(search); // 输出:"?search=test"
  1. window.location.hash:返回URL的锚部分,以"#"开头。

例如:

javascript
var hash = window.location.hash; console.log(hash); // 输出:"#section1"
  1. window.location.assign(url):加载新的文档。

  2. window.location.reload():重新加载当前页面。

  3. window.location.replace(url):用新的页面替换当前页面。

使用这些属性和方法,您可以根据需要获取和操作当前页面的URL。例如,如果您需要根据URL的查询字符串参数来执行某些操作,您可以提取 window.location.search,然后解析这些参数。

2024年6月29日 12:07 回复

使用纯 JavaScript 可以轻松获取当前页面的完整 URL 。例如,在此页面上尝试以下代码:

window.location.href; // use it in the console of this page will return // http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser"

window.location.href属性返回当前页面的 URL。

document.getElementById("root").innerHTML = "The full URL of this page is:" + window.location.href;

2024年6月29日 12:07 回复

使用:

shell
window.location.href

正如评论中所指出的,下面的行可以工作,但在 Firefox 中存在错误。

shell
document.URL

请参阅**DOMString 类型的 URL,readonly**。

2024年6月29日 12:07 回复

URL信息访问

JavaScript 为您提供了许多方法来检索和更改显示在浏览器地址栏中的当前 URL。所有这些方法都使用Locationobject,它是对象的属性Window。您可以Location通过以下方式读取当前对象window.location

shell
var currentLocation = window.location;

基本 URL 结构

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

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

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

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

  • **搜索:**查询字符串跟随路径组件,并提供资源可用于某种目的的一串信息(例如,作为搜索参数或作为要处理的数据)。

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

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

  • href - 整个 URL
  • 协议- URL 的协议
  • host - URL 的主机名和端口
  • 主机名- URL 的主机名
  • port - 服务器用于 URL 的端口号
  • pathname - URL 的路径名
  • search - URL 的查询部分
  • hash - URL 的锚点部分
  • 起源——window.location.protocol + '//' + window.location.host
2024年6月29日 12:07 回复

获取当前页面的URL:

shell
window.location.href
2024年6月29日 12:07 回复

你的答案