JavaScript中的location、history、navigator对象实例介绍(js location replace)这样也行?

随心笔谈9个月前发布 admin
214 00
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

文章摘要

文章介绍了使用JavaScript的`window.location`属性获取和操作网页地址的方法。文章详细讲解了如何通过`window.location`获取网页的地址信息,包括`hash`(路径后缀)、`host`(主机名)、`hostname`(域名)、`pathname`(文件路径)、`port`(端口号)、`protocol`(协议类型)以及`search`(搜索内容)。此外,文章还介绍了如何通过`location.href`方法设置跳转页面的地址,`location.assign`方法用于赋值操作,以及`location.reload()`和`location.replace()`方法用于刷新页面或替换历史记录。这些知识对于开发者掌握网页地址的操作和管理具有重要意义。

//地址栏上#及后面的内容
console.log(window.location.hash);
//主机名及端口号
console.log(window.location.host);
//主机名
console.log(window.location.hostname);
//文件的路径—相对路径
console.log(window.location.pathname);
//端口号
console.log(window.location.port);
//协议
console.log(window.location.protocol);
//搜索的内容
console.log(window.location.search);
//设置跳转的页面的地址
location.href=”http://www.jb51.net”;//属性—————–>必须记住
location.assign(“http://www.jb51.net”);//方法
//重新加载–刷新
location.reload();
//没有历史记录
location.replace(“http://www.jb51.net”);

© 版权声明

相关文章