window.location用法

本文详细介绍了JavaScript中window.location对象的各个属性及其使用方法,包括如何获取和设置URL的不同部分,如主机名、端口、路径、搜索参数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        // location (地址栏对象信息)
        //http://127.0.0.1:5500/04.location.html
        console.log(window.location);

        //host:域名和端口号
        console.log(window.location.host);

        //hostname:域名
        console.log(window.location.hostname);

        //href:完整的url地址
        //给值就是设置不给值就是获取
        console.log(window.location.href);
        // 网页跳转
        // window.location.href = "https://www.baidu.com";

        // port 端口号
        console.log(window.location.port); //5500

        //protocol  协议
        console.log(window.location.protocol); //http:

        //pathname 路径地址
        console.log(window.location.pathname); ///04.location.html

        // http://127.0.0.1:5500/04.location.html?name=%E5%93%88%E5%93%88&age=100#heihie
        //hash 哈希值  #..
        console.log(window.location.hash); //#heihie

        //search 获取通过?传递参数
        console.log(window.location.search); //?name=%E5%93%88%E5%93%88&age=100
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值