正文

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

文章摘要

文章介绍了使用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”);