location.href: 链接跳转
eg:点击按钮跳转到百度
Code:
<body> <button id="btn">跳转到百度</button> <script> var btn = document.getElementById("btn"); btn.onclick=function(){ location.href = "https://www.baidu.com"; } </script></body>
location.host:返回服务器名称和端口号
location.hash:返回URL中的hash(#号后跟0或多个字符),如果不包含则返回空字符串
location.hostname:返回不带端口号的服务器名称
location.pathname:返回URL中目录和(或)文件名
location.port:返回URL中指定的端口号,如果没有,返回空字符串
Code:
<body> <button id="btn">跳转到百度</button> <script> var btn = document.getElementById("btn"); btn.onclick=function(){ // location.href = "https://www.baidu.com"; console.log(location.host) //127.0.0.1:5500 console.log(location.hash) //空字符串 console.log(location.hostname) //127.0.0.1 console.log(location.pathname) //01.html console.log(location.port) //5500 } </script></body>