location.href: 链接跳转

eg:点击按钮跳转到百度

Code:

  1. <body>
  2. <button id="btn">跳转到百度</button>
  3. <script>
  4. var btn = document.getElementById("btn");
  5. btn.onclick=function(){
  6. location.href = "https://www.baidu.com";
  7. }
  8. </script>
  9. </body>

location.host:返回服务器名称和端口号

location.hash:返回URL中的hash(#号后跟0或多个字符),如果不包含则返回空字符串

location.hostname:返回不带端口号的服务器名称

location.pathname:返回URL中目录和(或)文件名

location.port:返回URL中指定的端口号,如果没有,返回空字符串

eg:以(http://127.0.0.1:5500/01.html)为server

Code:

  1. <body>
  2. <button id="btn">跳转到百度</button>
  3. <script>
  4. var btn = document.getElementById("btn");
  5. btn.onclick=function(){
  6. // location.href = "https://www.baidu.com";
  7. console.log(location.host) //127.0.0.1:5500
  8. console.log(location.hash) //空字符串
  9. console.log(location.hostname) //127.0.0.1
  10. console.log(location.pathname) //01.html
  11. console.log(location.port) //5500
  12. }
  13. </script>
  14. </body>