本文总结了在 JavaScript 中获取当前页面信息的常用命令,包括通过 document 和 location 对象获取 URL、域名、主机名及路径等关键信息,并指出了部分常见属性的使用方式与误区。
JavaScript 获取页面信息的方法
本文内容相对基础,旨在为大家总结在 JavaScript 中获取当前页面相关信息的一些常用命令。
console.info("document.URL=" + document.URL); console.info("document.location.href=" + document.location.href); //console.info("document.base=" + document.base); //这是错的! console.info("document.location=" + document.location); console.info("self.location.href=" + self.location.href); console.info("top.location.href=" + top.location.href); console.info("location.hostname=" + location.hostname); console.info("window.location.host=" + window.location.host); console.info("window.location.pathname=" + window.location.pathname);
目前想到的主要就这些,后续如有补充会再行更新。