Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js 获取url

作者:残缺的不完美   发布日期:2025-12-15   浏览:49

// 获取当前页面的完整 URL
const currentUrl = window.location.href;
console.log("当前页面的完整 URL 是: ", currentUrl);

// 获取 URL 中的协议 (例如: "http:" 或 "https:")
const protocol = window.location.protocol;
console.log("URL 的协议是: ", protocol);

// 获取 URL 中的主机名 (例如: "www.example.com")
const hostname = window.location.hostname;
console.log("URL 的主机名是: ", hostname);

// 获取 URL 中的端口号 (例如: "80" 或 "443")
const port = window.location.port;
console.log("URL 的端口号是: ", port);

// 获取 URL 中的路径部分 (例如: "/path/to/resource")
const pathname = window.location.pathname;
console.log("URL 的路径是: ", pathname);

// 获取 URL 中的查询参数 (例如: "?key1=value1&key2=value2")
const search = window.location.search;
console.log("URL 的查询参数是: ", search);

// 获取 URL 中的 hash 部分 (例如: "#section1")
const hash = window.location.hash;
console.log("URL 的 hash 部分是: ", hash);

解释说明:

  • window.location.href:返回当前页面的完整 URL。
  • window.location.protocol:返回 URL 的协议部分,例如 "http:""https:"
  • window.location.hostname:返回 URL 的主机名部分,例如 "www.example.com"
  • window.location.port:返回 URL 的端口号部分,例如 "80""443"。如果没有指定端口,则返回空字符串。
  • window.location.pathname:返回 URL 的路径部分,例如 "/path/to/resource"
  • window.location.search:返回 URL 中的查询参数部分,例如 "?key1=value1&key2=value2"
  • window.location.hash:返回 URL 中的 hash 部分,例如 "#section1"

这些属性可以帮助你从当前页面的 URL 中提取不同的部分,方便在 JavaScript 中进行处理。

上一篇:js 转义

下一篇:js html标签转义

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站