location 对象存储了当前文档位置(URL)相关的信息,简单地说就是网页地址字符串。使用 window 对象的 location 属性可以访问.
location对象提供了与当前窗口中加载的文档有关信息,还提供了一些导航的功能,他既是window对象的属性,也是document对象的属性。location.href与window.location.href等价语法:location.hash功能:返回URL中的hash(#号后跟0或多个字符),如果不包含则返回空字符串语法:location.host功能:返回服务器名称和端口号语法:location.hostname功能:返回不带端口号的服务器名称语法:location.pathname功能:返回URL中的目录和(或)文件名语法:location.port功能:返回URL中指定的端口号,如果没有,返回空字符串
<button id="btn">跳转到百度</button> <script> var btn = document.getElementById("btn"); btn.onclick = function(){ // location.href = "http://www.baidu.com"; //open in liver Server打开 console.log(location.host);//127.0.0.1:5500 console.log(location.hostname);//127.0.0.1 console.log(location.pathname);// /01.html console.log(location.port);//5500 } </script>